java - 绘制图像后设置的像素发生变化

标签 java image image-processing image-conversion

在我的项目中,获取图像的每个像素值,然后对像素执行解密并为像素设置新值。但是,在使用更改的像素值创建新图像后,新图像不能包含与我更改的像素值相同的像素值。主要像素值发生变化。如何解决这个问题呢。 我的代码是

     for( i=0;i<width;i++){
        for(int j=0;j<height;j++){

            //int p1 = pix[i][j];
            int a1 = ealph[i][j];           
            int r3 = ered[i][j];
            int g1 = ered[i][j];
            int b1 = ered[i][j];


            int p1 = (a1<<24) | (r3<<16) | (g1<<8) | b1;
                img.setRGB(i, j, p1);

        }

    }

    try{
        f = new File("C:\\Users\\chukkapalli\\workspace\\Des_Encryption\\decripted_img.jpg");
        ImageIO.write(img, "jpg", f);
      }catch(IOException e){
        System.out.println(e);
      }


  f = null;

 img = null;
    try{
          f = new File("C:\\Users\\chukkapalli\\workspace\\Des_Encryption\\decripted_img.jpg");
          img = ImageIO.read(f);
        }catch(IOException e){
          System.out.println(e);
        }
     width = img.getWidth();
     height = img.getHeight();

     for( i=0;i<width;i++){
            for(int j=0;j<height;j++){

                int p1 = img.getRGB(i,j);
                e1alph[i][j] = (p1>>24)&0xff;
                e1red[i][j] = (p1>>16)&0xff;
                egreen[i][j] = (p1>>8)&0xff;
                eblue[i][j] = p1&0xff;

                System.out.println("orginal image- "+ealph[i][j]+" "+ered[i][j]+" "+ered[i][j]+" "+ered[i][j]+" created -- "+e1alph[i][j]+" "+e1red[i][j]+" "+egreen[i][j]+" "+eblue[i][j]);

            }

        }

输出为

     orginal image- 0 33 33 33 created -- 255 35 35 35
     orginal image- 0 38 38 38 created -- 255 41 41 41
     orginal image- 0 140 140 140 created -- 255 142 142 142
     orginal image- 0 81 81 81 created -- 255 51 51 51
     orginal image- 0 22 22 22 created -- 255 21 21 21
     orginal image- 0 127 127 127 created -- 255 135 135 135
     orginal image- 0 126 126 126 created -- 255 127 127 127
     orginal image- 0 63 63 63 created -- 255 62 62 62
     orginal image- 0 244 244 244 created -- 255 250 250 250
     orginal image- 0 11 11 11 created -- 255 0 0 0
     orginal image- 0 13 13 13 created -- 255 11 11 11
     orginal image- 0 171 171 171 created -- 255 175 175 175
     orginal image- 0 126 126 126 created -- 255 130 130 130
     orginal image- 0 8 8 8 created -- 255 0 0 0
     orginal image- 0 6 6 6 created -- 255 14 14 14
     orginal image- 0 248 248 248 created -- 255 244 244 244
     orginal image- 0 109 109 109 created -- 255 128 128 128
     orginal image- 0 107 107 107 created -- 255 86 86 86
     orginal image- 0 50 50 50 created -- 255 48 48 48
     orginal image- 0 104 104 104 created -- 255 131 131 131
     orginal image- 0 110 110 110 created -- 255 95 95 95
     orginal image- 0 127 127 127 created -- 255 128 128 128

如何在创建图像后不更改像素值的情况下创建图像。 谢谢。

最佳答案

尝试使用无损格式,例如 PNG。 JPG 在压缩过程中改变了像素值,因此无法完全按照创建时的方式读取。

关于java - 绘制图像后设置的像素发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39240275/

相关文章:

html - CSS 显示调整大小和裁剪的图像

php获取图像名称并添加到数据库?

image-processing - 增强现实游戏

c++ - 在 C++ 中对 Kinect 生成的点进行分组

Java ANT 组合任务

java - 如何在 ICU4J 中合并音译规则?

html - 边框比页脚中的图像长

python - 试图理解 Django 的 sorl-thumbnail

java - 文件: byte arrays and dimentions的处理

java - 如何解决比较的一般错误?