java修改像素_java-为什么ImageIO.write()方法修改像素值?

我正在尝试运行一个简单的Java程序,尝试执行以下操作:从给定图像中提取像素数据.然后使用此数据创建相同类型的新图像.问题是,当我读取此创建图像的像素数据时,像素值与我写入的像素值不同.这种情况不仅发生在.jpg图像上,还发生在某些.png图像上(因此,它甚至不限于图像类型).

这是我的代码:

package com.alex;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Test {

public static void main(String[] args) {

try{

// Read source image

BufferedImage img = ImageIO.read(new File("D:/field.png"));

int width = img.getWidth();

int height = img.getHeight();

int[] imagePixels = new int[width*height];

img.getRGB(0, 0, width, height, imagePixels, 0, width);

// Create copy image

BufferedImage destImg = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());

destImg.setRGB(0, 0, img.getWidth(), img.getHeight(), imagePixels, 0, img.getWidth());

File out = new File("D:/test.png");

ImageIO.write(destImg, "png", out);

// Extract copy image pixels

BufferedImage copy = ImageIO.read(new File("D:/test.png"));

int width1 = copy.getWidth();

int height1 = copy.getHeight();

int[] extractedPixels = new int[width1*height1];

copy.getRGB(0, 0, width1, height1, extractedPixels, 0, width1);

System.out.println("The 2 dimensions are " + imagePixels.length + " " + extractedPixels.length );

// Compare the piels from the 2 images

int k=0;

for(int i=0; i

if(imagePixels[i] != extractedPixels[i]) {

k++;

}

}

System.out.println("Number of different pixels was: " + k);

}catch(IOException e) {

System.out.println("Exception was thrown during reading of image: " + e.getMessage());

}

}

}

不幸的是,这两个图像像素数据经常且不可预测地不同.有人可以帮我找到一种方法,以便至少对于图像类型不修改值吗?

编辑这里是上述过程中失败的图像

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值