java - BufferedImage 灰度图太暗
问题描述
我的任务是将 png 格式的灰度图像读取到二维数组,并对每个像素进行一些算术运算。我写了一些代码来读取我的图像并将其转换为二维数组,但结果我的图像更暗,即使我还没有改变任何东西。我的代码:
BufferedImage image = ImageIO.read(new File(IMAGE_PATH + "gray/squirrel.png"));
int width = image.getWidth();
int height = image.getHeight();
int[][] imageData = new int[width][height];
for(int i = 0; i < width ; ++i){
for(int j = 0 ; j < height ; ++j){
Color color = new Color(image.getRGB(i,j));
imageData[i][j] = (color.getRed() + color.getGreen() + color.getBlue()) / 3;
}
}
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
for (int x = 0; x < width ; x++) {
for (int y = 0; y < height; y++) {
newImage.setRGB(x, y, imageData[x][y]);
}
}
File outputfile = new File("image.png");
ImageIO.write(newImage, "png", outputfile);
解决方案
推荐阅读
- python - 按键值过滤字典列表 - 将答案作为字典列表返回
- java - 如何使用 htmlunit 从 html div 元素中使用 role="alert" 捕获文本
- r - 检查和安装缺失包的函数输出
- sql - 通过将 count () 更改为 (partition by ) 结果集来排序
- django - heroku 在本地工作但不在服务器中显示“错误:连接到 localhost:5037”
- vim - 如何为 Eigen 库创建标签并将它们与 OmniCppComplete 一起使用?
- python - 如何为 ARM 交叉编译单个 python 库
- react-native - Expo 提供哪些 RSA 公钥加密选项?
- java - 无法启动活动 ComponentInfo java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法
- html - Google Adsense 在我的博主上找不到代码