绘制图像后,设置像素会发生变化

时间:2016-08-31 03:29:43

标签: 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

如何在创建后创建没有更改像素值的图像。 谢谢。

1 个答案:

答案 0 :(得分:1)

尝试使用像PNG这样的无损格式。 JPG更改其压缩中的像素值,并且无法像创建时那样完全读取。