Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java灰度缓冲图像_Java_Image_Bufferedimage_Grayscale - Fatal编程技术网

Java灰度缓冲图像

Java灰度缓冲图像,java,image,bufferedimage,grayscale,Java,Image,Bufferedimage,Grayscale,所以我有一个字节数组,表示像素数据(8位灰度)。没有标题。没什么。只是数据。我想从这个数据创建一个缓冲图像。是的 image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); image.getRaster().setDataElements(0, 0, w, h, data); this.x = w; this.y = h; scalemode=false; exactmode=true; 其中,w是像素宽度,h是像素高度,数据

所以我有一个字节数组,表示像素数据(8位灰度)。没有标题。没什么。只是数据。我想从这个数据创建一个缓冲图像。是的

image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setDataElements(0, 0, w, h, data);
this.x = w;
this.y = h;
scalemode=false;
exactmode=true;
其中,w是像素宽度,h是像素高度,数据是字节数组,图像是缓冲图像

这是我的作画方法

 protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        int rx = (this.getWidth() - x) / 2;
        int ry = (this.getHeight() - y) / 2;

        g2d.drawImage(image, rx, ry,x,y, null);
    }
一、 但是,获取此图像(真实图像是指纹,大部分为白色像素)

出了什么问题?我试着按原样保存数据,然后在Photoshop中查看。数据很好

[编辑]
不要管这个问题。我在代码的另一部分搞砸了,我不知道。感谢您的所有输入,尽管

在每个像素上绘制每个字节

BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
for (int dy = 0; dy < h; dy ++){
    for(int dx = 0; dx < w; dx ++){
        int index = dy*w + dx;
        int rgb = data[index];
        rgb = rgb << 24 & 0xFFFF; //BufferedImage.TYPE_BYTE_GRAY consideres only the red-channel;
        //rgb = 00 data[index] FF FF
        image.setRGB(dx,dy,rgb);
        }
    }
}
BufferedImage image=新的BufferedImage(w,h,BufferedImage.TYPE_BYTE_GRAY);
对于(int-dy=0;dyrgb=rgb您没有正确设置缓冲区

byte[] data = ... 
DataBufferByte db = new DataBufferByte(data, w*h);
image.getRaster().setDataElements(0, 0, w, h, db );

请参见

由于您没有发布足够的信息,因此很难确切地知道错误所在。我们不知道
w
h
或者您的
数据中包含哪些信息。我们不知道图像应该是什么样子

但是,这里有一些代码,它与您正在做的几乎完全相同,对我来说也很有用:

// Set up h/w and backing data
int w = 300;
int h = 200;
byte[] data = new byte[w * h];

// Create a smooth gradient
for (int y = 0; y < h; y++) {
    int off = y * w;

    for (int x = 0; x < w; x++) {
        data[off + x] = (byte) (Math.round((x / (double) w) * 127) 
                              + Math.round((y / (double) h) * 127));
    }
}

// Create BufferedImage from data
final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setDataElements(0, 0, w, h, data);

// Show it all in a window
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        JFrame frame = new JFrame(getClass().getSimpleName());
        frame.add(new JLabel(new ImageIcon(image)));

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
});
//设置h/w和备份数据
int w=300;
int h=200;
字节[]数据=新字节[w*h];
//创建平滑的渐变
对于(int y=0;y
结果如下:


什么类型是
数据
?数据是byte[]1byte=1pixeldraw每个像素上的每个字节…这是图像的1像素边框部分吗?没有边框不是图像的一部分您要寻找的方法是。引用文档:“从类型TransferType的基本数组中为像素矩形设置数据”(重点是我的)。这个答案显然是错误的。OP的代码在这方面没有问题。@haraldK是的,请继续阅读,您会发现“对于Java 2D API支持的图像数据,这将是DataBuffer.TYPE\u BYTE、DataBuffer.TYPE\u USHORT、DataBuffer.TYPE\u INT、DataBuffer.TYPE\u SHORT、DataBuffer.TYPE\u FLOAT或DataBuffer.TYPE\u DOUBLE中的一个”…那么错误在哪里呢?你似乎认为
DataBuffer.TYPE_BYTE
=
DataBufferByte
。它不是。一个“TYPE
DataBuffer.TYPE_BYTE
”的原始数组只是指
字节[]
。不,这是我现在想的^^(DataBufferByte.getType())将返回DataBuffer.TYPE_BYTE-但这将导致无where)…我将解释您的示例,并接受您的代码正在工作。正如您示例中的一条注释所述,我猜OP只是不知道宽度/高度的问题…@haraldK感谢您提供了一个好的示例…我也这么认为。这就是为什么我询问边界的原因。并且,记录在案,您上面的代码将在运行时崩溃,而f以下异常:
java.lang.ClassCastException:java.awt.image.DataBufferByte无法强制转换为[B
;-)Green Wall提供的图像看起来宽度和高度的值不正确。我刚刚尝试测试了整个过程,发现如果图像是在一个很好的主线程中创建的,那么图像就可以了。但是,当我在鼠标事件侦听器线程中创建图像时,图像变得混乱!我刚刚意识到数据(字节数组)已经搞砸了,所以我无法正确处理。您知道从侦听器线程读取BufferedinputStream时可能会出现什么问题吗?我使用了中的代码。尤其是upImgmethod@GreenMellon好的。没问题。:-)从流中读取应该和从侦听器线程正常工作一样,但是为了响应强烈建议不要使用UI。一般来说,所有I/O都应该在后台线程上完成。无论如何,很高兴你找到了它!原来我使用的指纹模块在将图像转换为签名文件时实际上修改了它的图像缓冲区。我完全不知道为什么(内存小?).所以它与Java无关。不过谢谢你的提示