如何使用OpenCV Java压缩图像并存储到特定文件夹

时间:2022-04-24 21:23:31

Hi I want to compress and store compressed image to folder. So I have used the below code,

嗨,我想压缩和存储压缩图像到文件夹。所以我使用了下面的代码,

import java.io.*;
import java.util.*;
import java.awt.image.*;

import javax.imageio.*;
import javax.imageio.stream.ImageOutputStream;

public class Compression {

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

        String dc = "C:\\Users\\admin\\Desktop\\RFI\\DC\\1_1_c.jpg";
        String dr = "C:\\Users\\admin\\Desktop\\RFI\\DR";

        File file = new File(dc);
        BufferedImage image = ImageIO.read(file);
        OutputStream os =new FileOutputStream(new File(dr));
        Iterator<ImageWriter>writers =  ImageIO.getImageWritersByFormatName("jpg");
        ImageWriter writer = (ImageWriter) writers.next();

        ImageOutputStream ios = ImageIO.createImageOutputStream(os);
        writer.setOutput(ios);

        ImageWriteParam param = writer.getDefaultWriteParam();

        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(0.05f);
        writer.write(null, new IIOImage(image, null, null), param);

        os.close();
        ios.close();
        writer.dispose();
    }
}

But i'm not get compressed image. Only get the below error on console

但我不会得到压缩图像。只在控制台上获得以下错误

Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at com.opencv.Compression.main(Compression.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Please suggest me any idea....

请建议我任何想法....

1 个解决方案

#1


1  

This would deserve a comment more than an answer but since my reputation is so low, I can't comment and am forced to write it as an answer.

这应该得到的评论不仅仅是一个答案,但由于我的声誉如此之低,我无法评论,我不得不把它作为答案。

Did you try to understand the error you get ? The call stack is pretty clear. You have an error at the line BufferedImage image = ImageIO.read(file);. The program can't find your image. Did you make sure that the image file specified in String dc = "C:\\Users\\admin\\Desktop\\RFI\\DC\\1_1_c.jpg"; actually existed ?

你试图理解你得到的错误吗?调用堆栈非常清楚。 BufferedImage image = ImageIO.read(file);行有错误。该程序找不到您的图像。您是否确保在String dc =“C:\\ Users \\ admin \\ Desktop \\ RFI \\ DC \\ 1_1_c.jpg”中指定的图像文件;实际存在?

#1


1  

This would deserve a comment more than an answer but since my reputation is so low, I can't comment and am forced to write it as an answer.

这应该得到的评论不仅仅是一个答案,但由于我的声誉如此之低,我无法评论,我不得不把它作为答案。

Did you try to understand the error you get ? The call stack is pretty clear. You have an error at the line BufferedImage image = ImageIO.read(file);. The program can't find your image. Did you make sure that the image file specified in String dc = "C:\\Users\\admin\\Desktop\\RFI\\DC\\1_1_c.jpg"; actually existed ?

你试图理解你得到的错误吗?调用堆栈非常清楚。 BufferedImage image = ImageIO.read(file);行有错误。该程序找不到您的图像。您是否确保在String dc =“C:\\ Users \\ admin \\ Desktop \\ RFI \\ DC \\ 1_1_c.jpg”中指定的图像文件;实际存在?