我想读取图像并将其输出为新的灰度和棕褐色版本。我已经找出了大部分,但转换只适用于某些图像。对于其他人来说,这只是导致的错误信息:Java将图像输出为灰度和棕褐色
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:918)
at ChangeColor.color2gray(ChangeColor.java:64)
at ChangeColor.main(ChangeColor.java:129)
我认为这是与图像的RGB值,但我不知道如何来调整,使任何此代码的工作图片。将不胜感激,谢谢。
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
public class ChangeColor{
static BufferedImage readImage(String fname) throws Exception {
BufferedImage image = ImageIO.read(new File(fname));
return(image);
}
public static void saveImage(BufferedImage img, File file) throws IOException {
ImageWriter writer = null;
java.util.Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if(iter.hasNext()){
writer = (ImageWriter)iter.next();
}
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
writer.setOutput(ios);
ImageWriteParam param = new JPEGImageWriteParam(java.util.Locale.getDefault());
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
param.setCompressionQuality(0.98f);
writer.write(null, new IIOImage(img, null, null), param);
}
public static BufferedImage color2gray(BufferedImage inImage) {
int width = inImage.getWidth();
int height = inImage.getHeight();
BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for(int i=0; i
for(int j=0; j
{
int pic= inImage.getRGB(i,j);
int in_r = ((pic>>16) & 0xFF);
int in_g = ((pic>>8) & 0xFF);
int in_b = (pic & 0xFF);
float gray = (float)(in_r * 0.2126 + in_g * 0.7152 + in_b * 0.0722)/256;
Color color = new Color (gray, gray, gray);
int RGB = color.getRGB();
outImage.setRGB (i,j,RGB);
}
}
}
return(outImage);
}
public static BufferedImage color2sepia(BufferedImage inImage) {
int width = inImage.getWidth();
int height = inImage.getHeight();
BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for(int i=0; i
for(int j=0; j
{
int pic= inImage.getRGB(i,j);
int in_r = ((pic>>16) & 0xFF);
int in_g = ((pic>>8) & 0xFF);
int in_b = (pic & 0xFF);
float out_r = (float)(((in_r * .393) + (in_g *.769) + (in_b * .189))/256);
if (out_r>1)
out_r = 1;
float out_g = (float)(((in_r * .349) + (in_g *.686) + (in_b * .168))/256);
if (out_g>1)
out_g = 1;
float out_b = (float)(((in_r * .272) + (in_g *.534) + (in_b * .131))/256);
if (out_b>1)
out_b = 1;
Color color = new Color (out_r, out_g, out_b);
int RGB = color.getRGB();
outImage.setRGB (i,j,RGB);
}
}
}
return(outImage);
}
public static void main(String[] args) throws Exception {
BufferedImage colorImage, grayImage, sepiaImage;
if (args.length != 1)
System.out.println("usage is: java ChangeColor filename");
else
{
colorImage = readImage (args[0]);
grayImage = color2gray (colorImage);
sepiaImage = color2sepia(colorImage);
saveImage(grayImage, new File("gray" + args[0]));
saveImage(sepiaImage, new File("sepia"+ args[0]));
}
}
}
+1
定义“不起作用”。这是任何问题的基本要求信息。 –
+0
请定义你的意思是不起作用? –
+2
错误信息是我读过的最易理解的信息之一。你为什么不读它?它会告诉你确切的错误和错误。 –