Java 以较少的压缩损失保存缓冲图像

Java 以较少的压缩损失保存缓冲图像,java,Java,我有一个java程序,它使用java Robot类定期捕获屏幕截图。但由于它经常(约5秒)截图,所以很快就会填满我的硬盘。 是否有任何方法可以在保存之前减小图像的大小,但可以在不丢失质量的情况下重新生成原始图像 import java.io.*; import java.util.*; import java.awt.*; import java.io.*; import java.awt.image.BufferedImage; import java.awt.*; import com.my

我有一个java程序,它使用java Robot类定期捕获屏幕截图。但由于它经常(约5秒)截图,所以很快就会填满我的硬盘。 是否有任何方法可以在保存之前减小图像的大小,但可以在不丢失质量的情况下重新生成原始图像

import java.io.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
import java.sql.*;  
import java.util.*;
import javax.imageio.ImageIO;
class wtd
{
public static BufferedImage getImage()throws Exception
       {
            Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
              Robot robot = new Robot();


               BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, (int) screenDim.getWidth(),(int) screenDim.getHeight()));
               return image;



    }
      public static void main(String args[])throws Exception
    { 
                 long id=0;
                       try{
                           while(true)
                           {
                       BufferedImage originalImage=getImage();

                          ImageIO.write(originalImage, "jpg", new File("D:/"+id+".jpg"));
                                         id++;


                                 }
                   }

               catch(Exception e){}

            }
        }

如果您的图像已经被压缩,使用其他压缩处理将不会有很大帮助:您将节省空间,但不一定会有很多。

如果您的图像没有压缩或压缩很少,您可以使用经典的压缩工具,如zip,它允许无损数据压缩
您有多个压缩级别,但压缩级别越高,处理时间越长。
因此,根据cpu功率、可用cpu线程数和图像大小,您应该使用或多或少重要的压缩级别。

例如,该类允许通过调用
setLevel(int-level)
方法来创建具有特定压缩级别的zip。
然后可以使用
java.util.zip.ZipInputStream
类来提取归档文件。


编辑代码示例:

下面是一个未经测试的示例,它使用
javax.imageio.imageio.write()
JDK 8特定方法,允许将
java.awt.image.buffereImage
写入
java.io.OutputStream
对象:

  // Here is the capture bufferedImage from your application
  BufferedImage screenShot = ...;

  // You create the zip file and you add entry that will store the image
  FileOutputStream fileOut = new FileOutputStream("yourZipFile.zip");
  ZipOutputStream zipOut = new ZipOutputStream(fileOut);
  zipOut.setLevel(9); // 9 is the max level      
  ZipEntry zipEntry = new ZipEntry("screenshot-2017-03-24_12-03-30.jpg");
  zipOut.putNextEntry(zipEntry);

  // you get the bytes from the image 
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  javax.imageio.ImageIO.write(screenShot, "jpg", out);
  byte[] bytes = out.toByteArray();

  // you write the bytes in the zipOutputStream
  zipOut.write(bytes, 0, bytes.length);
  zipOut.close();

如果您正在寻找无损压缩,您可以尝试以下方法:

通过预测JPEG块中的系数并将这些预测作为上下文输入算术编码器,现有JPEG图像可节省22%。Lepton一点一点地完美地保留了原始文件。它以每秒5兆字节的速度压缩JPEG文件,并以每秒15兆字节的速度将其解码回原始位,安全、可靠且内存不足24兆字节


你能提供你的代码吗?是的,请等一分钟。“…在保存之前减小图像的大小,但可以在不丢失质量的情况下重新生成原始图像”如果你能做到这一点(除了保存为png或其他格式),我想全世界都会想知道的@史蒂文·史密斯我认为这是可能的,我听说过无损压缩这个术语。也许你可以将图片保存在第一张图片之后,作为两张图片之间的差异?java.util.zip.ZipOutputStream可以压缩一个BuffereImage吗?或者首先我必须保存它,然后压缩保存的文件并重新保存压缩的文件。你不需要将图像保存在文件我将用一个例子来编辑我的答案。我想知道的是,它可以直接压缩缓冲图像,或者我必须先将其保存为jpeg文件,然后我必须压缩并重新保存它。