2017-09-29 96 views
0

我有一個256 * 256維度的灰度圖像。我試圖將其縮小到128 * 128。 我正在取兩個像素的平均值並將其寫入輸出文件。無法縮小灰度圖像

class Start { 

public static void main (String [] args) throws IOException { 

File input= new File("E:\\input.raw"); 

File output= new File("E:\\output.raw"); 
new Start().resizeImage(input,output,2); 

} 



public void resizeImage(File input, File output, int downScaleFactor) throws IOException { 
      byte[] fileContent= Files.readAllBytes(input.toPath()); 
      FileOutputStream stream= new FileOutputStream(output); 
      int i=0; 
      int j=1; 
      int result=0; 
      for(;i<fileContent.length;i++) 
      { 
       if(j>1){ 
        // skip the records. 
        j--; 
        continue; 
       } 
       else { 
        result = fileContent[i]; 
        for (; j < downScaleFactor; j++) { 
         result = ((result + fileContent[i + j])/2); 
        } 
        j++; 
        stream.write(fileContent[i]); 
       } 
      } 
     stream.close(); 
     } 

} 

以上代碼成功運行,我可以看到輸出文件大小的尺寸減小,但是當我嘗試轉換 輸出文件(原始文件)網上JPG(https://www.iloveimg.com/convert-to-jpg/raw-to-jpg)它給我一個錯誤說,文件已損壞。 我已經從相同的在線工具轉換輸入文件,它完美的工作。我的代碼正在創建損壞的文件有問題。 我該如何糾正它?

P.S我不能使用任何直接縮小圖像的庫。

+1

我會建議在看看[這個問題](https://stackoverflow.com/questions/14115950/quality-of-image-後調整大小非常低java)討論縮小圖像和保持質量。因爲接受答案使用第三方庫,我還建議看看[這個答案](https://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background- image/11959928#11959928)討論了使用分而治之的方法來使用Java本身的庫功能來縮放圖像 – MadProgrammer

+0

您正在鏈接的在線工具轉換[「Camera RAW」](https://en.wikipedia.org/ wiki/Raw_image_format)文件轉換爲JPEG格式。這些類型的文件不是「原始」像素數據,它們符合文件格式(通常基於TIFF/Exif),包含不同壓縮/分辨率的多個圖像,縮略圖等以及「原始」傳感器數據(通常是JPEG無損壓縮)。您的代碼假定該文件包含原始像素數據,並且會損壞該進程中的相機RAW文件。看到[這個答案](https://stackoverflow.com/q/1222324/1428606)有關如何閱讀它們的一些輸入... – haraldK

回答

0

您的代碼沒有處理圖片大小調整。

請參閱how-to-resize-images-in-java

哪個,我在這裏複製一個簡單的版本:

import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 

public class ImageResizer { 

    public static void resize(String inputImagePath, 
      String outputImagePath, int scaledWidth, int scaledHeight) 
      throws IOException { 
     // reads input image 
     File inputFile = new File(inputImagePath); 
     BufferedImage inputImage = ImageIO.read(inputFile); 

     // creates output image 
     BufferedImage outputImage = new BufferedImage(scaledWidth, 
       scaledHeight, inputImage.getType()); 

     // scales the input image to the output image 
     Graphics2D g2d = outputImage.createGraphics(); 
     g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null); 
     g2d.dispose(); 

     // extracts extension of output file 
     String formatName = outputImagePath.substring(outputImagePath 
       .lastIndexOf(".") + 1); 

     // writes to output file 
     ImageIO.write(outputImage, formatName, new File(outputImagePath)); 
    } 

    public static void resize(String inputImagePath, 
      String outputImagePath, double percent) throws IOException { 
     File inputFile = new File(inputImagePath); 
     BufferedImage inputImage = ImageIO.read(inputFile); 
     int scaledWidth = (int) (inputImage.getWidth() * percent); 
     int scaledHeight = (int) (inputImage.getHeight() * percent); 
     resize(inputImagePath, outputImagePath, scaledWidth, scaledHeight); 
    } 

    public static void main(String[] args) { 
     String inputImagePath = "resources/snoopy.jpg"; 
     String outputImagePath1 = "target/Puppy_Fixed.jpg"; 
     String outputImagePath2 = "target/Puppy_Smaller.jpg"; 
     String outputImagePath3 = "target/Puppy_Bigger.jpg"; 

     try { 
      // resize to a fixed width (not proportional) 
      int scaledWidth = 1024; 
      int scaledHeight = 768; 
      ImageResizer.resize(inputImagePath, outputImagePath1, scaledWidth, scaledHeight); 

      // resize smaller by 50% 
      double percent = 0.5; 
      ImageResizer.resize(inputImagePath, outputImagePath2, percent); 

      // resize bigger by 50% 
      percent = 1.5; 
      ImageResizer.resize(inputImagePath, outputImagePath3, percent); 

     } catch (IOException ex) { 
      System.out.println("Error resizing the image."); 
      ex.printStackTrace(); 
     } 
    } 

} 
+1

Java的縮放是衆所周知的低質量,這可以通過使用分而治之的方法來改善,展示[這裏](https://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) – MadProgrammer