2014-02-20 52 views
-1

我想讀取圖像並將其輸出爲新的灰度和棕褐色版本。我已經找出了大部分,但轉換隻適用於某些圖像。對於其他人來說,這只是導致的錯誤信息: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<height; i++){ 
       for(int j=0; j<width; 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<height; i++){ 
       for(int j=0; j<width; 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

請定義你的意思是不起作用? – Tanay

+2

錯誤信息是我讀過的最易理解的信息之一。你爲什麼不讀它?它會告訴你確切的錯誤和錯誤。 – jarnbjo

回答

0

你的錯誤是在這裏:

 for(int i=0; i<height; i++){ 
      for(int j=0; j<width; j++){ 
      { 
       int pic= inImage.getRGB(i,j); 

你交換的寬度和高度,所以除非你的形象是一個完美的廣場,你會得到您發佈的錯誤。