无法缩小灰度图像

时间:2017-09-29 00:21:41

标签: java image-processing

我有一个尺寸为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 online(https://www.iloveimg.com/convert-to-jpg/raw-to-jpg)它给我一个错误,说文件已损坏。 我已经从相同的在线工具转换输入文件,它正在完美地工作。我的代码创建了损坏的文件有问题。 我该如何纠正?

P.S我不能使用任何直接缩小图像尺寸的库。

1 个答案:

答案 0 :(得分: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();
        }
    }

}