2017-09-29 21 views
0

サイズ256 * 256のグレースケール画像があります。これを128 * 128にダウンスケールしようとしています。 私は平均2ピクセルをとり、出力ファイルに書き込みます。コードの実行上記スケールグレースケール画像を縮小できません

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(); 
     } 

} 

に成功し、それがいることを言って私にエラーを与えている私は、出力ファイルサイズの大きさが小さくなる見ることができますが、私はオンライン(https://www.iloveimg.com/convert-to-jpg/raw-to-jpg)をJPGする 出力ファイル(RAWファイル)を変換しようとすると、ファイルが破損しています。 入力ファイルを同じオンラインツールから変換しています。完全に動作しています。壊れたファイルを作成している私のコードで何かが間違っています。 どうすれば修正できますか?

P.Sイメージを直接縮小するライブラリは使用できません。

+1

を縮小後の画像と品質を維持するための議論については、後のサイズ変更(非常に低いJava)を参照してください。受け入れの回答ではサードパーティライブラリが使用されるため、[この回答](https://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-画像/ 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を参照してください。

私はここに簡単なバージョンにコピーしています、

:私は、[この質問](https://stackoverflow.com/questions/14115950/quality-of-image-を見た推薦

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

関連する問題