Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 如何从存储为r、g、b的像素创建图像,每个像素在阵列中占据不同的位置?_Java_Image Processing_Byte_Bufferedimage - Fatal编程技术网

Java 如何从存储为r、g、b的像素创建图像,每个像素在阵列中占据不同的位置?

Java 如何从存储为r、g、b的像素创建图像,每个像素在阵列中占据不同的位置?,java,image-processing,byte,bufferedimage,Java,Image Processing,Byte,Bufferedimage,我正在读取.jpg图像,并将像素设置为: if (type == BufferedImage.TYPE_3BYTE_BGR) { System.out.println("type.3byte.bgr"); byte[] pixels = (byte[]) sourceImage.getData().getDataElements(0, 0, w, h, null); } //处理这个称为像素的数组并显示结果图像 //首先我把它转换成整数 int offseet=0; int[

我正在读取.jpg图像,并将像素设置为:

if (type == BufferedImage.TYPE_3BYTE_BGR) {
   System.out.println("type.3byte.bgr");
   byte[] pixels = (byte[]) sourceImage.getData().getDataElements(0, 0, w, h, null);
}
//处理这个称为像素的数组并显示结果图像 //首先我把它转换成整数

int offseet=0;
   int[] data=new int[width*height*3];
   for ( i = 0; i < data.length; i++) { 
       data[i] = pixels[offset++] & 0xff;
     }
//对于penguins.jpg图像(在windows 7的示例图片中提供)

我得到的输出是


我不知道您的代码是如何运行的,因为它不会像jcomeau_ictx指出的那样编译。问题是,您使用不同的图像类型进行读写。 下面是一个代码片段,它将生成与输入相同的图像

public static void main(String[] args) {

        BufferedImage sourceImage = null;
        try {
            sourceImage = ImageIO.read(new File("IScSH.jpg"));
        } catch (IOException e) {
        }

        int type = sourceImage.getType();
        int w = sourceImage.getWidth();
        int h = sourceImage.getHeight();
        byte[] pixels = null;
        if (type == BufferedImage.TYPE_3BYTE_BGR) {
            System.out.println("type.3byte.bgr");
            pixels = (byte[]) sourceImage.getData().getDataElements(0, 0, w, h, null);
        }
        try {
            BufferedImage edgesImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
            edgesImage.getWritableTile(0, 0).setDataElements(0, 0, w, h, pixels);
            ImageIO.write(edgesImage, "jpg", new File("nvRhP.jpg"));
        } catch (IOException e) {
        }
    }

在初始值设定项中将“offset”改为“offseet”。为了更快地提供帮助,请发布一个。(第二张图片非常棒,但几乎是艺术作品。);@谢谢,先生,但问题是我正在将字节数组转换为整数数组,因为我需要对像素进行一些处理。因此,我的新整数数组显示上面的蓝色图像。在try块代码中,整数的类型不能是type\u 3BYTE\u BGR,对吗?请告诉我为什么原始图像和新构造的图像之间存在大小差异?还有type\u INT\u BGR类型。图像大小可能取决于几个因素,例如压缩比。如果图像看起来像你想要的,那么我认为你不必在意它。
public static void main(String[] args) {

        BufferedImage sourceImage = null;
        try {
            sourceImage = ImageIO.read(new File("IScSH.jpg"));
        } catch (IOException e) {
        }

        int type = sourceImage.getType();
        int w = sourceImage.getWidth();
        int h = sourceImage.getHeight();
        byte[] pixels = null;
        if (type == BufferedImage.TYPE_3BYTE_BGR) {
            System.out.println("type.3byte.bgr");
            pixels = (byte[]) sourceImage.getData().getDataElements(0, 0, w, h, null);
        }
        try {
            BufferedImage edgesImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
            edgesImage.getWritableTile(0, 0).setDataElements(0, 0, w, h, pixels);
            ImageIO.write(edgesImage, "jpg", new File("nvRhP.jpg"));
        } catch (IOException e) {
        }
    }