JAVA在写入时出错,java – 写入灰度图像时出错

以下是我编写的代码,用于更改灰度图像的像素值以创建全白图像(所有像素值设置为255).但是,如图2所示,我没有得到完整的白色图像,而是在它们之间得到黑色垂直条.我哪里出错了?

package dct;

import java.awt.image.BufferedImage;

import java.awt.image.WritableRaster;

import java.io.File;

import javax.imageio.ImageIO;

public class writeGScale {

public static void main(String[] args){

File file = new File("bridge.jpg");

BufferedImage img = null;

try{

img = ImageIO.read(file);

}

catch(Exception e){

e.printStackTrace();

}

int width = img.getWidth();

int height = img.getHeight();

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

WritableRaster raster1=img.getRaster();

for(int i=0;i

for(int j=0;j

tempArr[0] = 255;

raster1.setPixel(i, j, tempArr);

}

}

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

image.setData(raster1);

try{

File ouptut = new File("change.png");

ImageIO.write(image, "png", ouptut);

}

catch(Exception e){

e.printStackTrace();

}

}

}

最佳答案 我不知道为什么你将大数组tempArr = new int [width * height]传递给setpixel方法.i创建了一个表示白色的int col数组,我将它传递给setPixel()方法然后我就能得到完整的白色图像作为输出.

int col[]={255,255,255};

WritableRaster raster1=img.getRaster();

for(int i=0;i

{

for(int j=0;j

{

raster1.setPixel(i, j, col);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值