有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    要创建每个像素大小为2x2的图片,必须缩放图像(系数2)以仅显示。。。或者,如果你想创建一个图像,你必须手动创建一个图像,并在其上绘制比例因子2

    int[] pixels = ... //we already have our gray scale pixels here
    int widthOriginal = ... //size of original image
    int heightOriginal = ...
    
    //let's create an buffered Image twice the size
    BufferedImage img = 
        new BufferedImage(2*widthOriginal, 2*heightOriginal, BufferedImage.TYPE_4BYTE_ABGR);
    
    //we paint on the buffered image's graphic...
    Graphics gr = img.getGraphics();
    
    //we draw all pixels on the graphic
    for(int y = 0; y < heightOriginal; y ++){
        for(int x = 0; x < widthOriginal; x ++){
            int index = y*widthOriginal + x;
            int gray = pixels[index];
    
            //to draw we first set the color
            gr.setColor(new Color(gray));
    
            //then draw the pixel
            gr.drawRect(2*x, 2*y,2,2); //draw a 2x2 pixel instead of a 1x1 pixel
        }
    }
    

    嗯——老实说,我完全是胡思乱想写的代码,所以可能会有一些小的编译问题。。。但是这项技术被正确地解释了