Improve the user experience and use java to process jpeg pictures into progressive pictures

Posted by algy on Sat, 04 Apr 2020 19:09:03 +0200

JPEG files can be saved in two ways: Baseline JPEG (standard) and Progressive JPEG (progressive).
The two formats have the same size and image data, and the extension is the same. The only difference is that they are displayed in different ways.

1. standard type

This kind of image is scanned line by line. When the image is large or the network download speed is slow, you will see the effect that the image is loaded line by line, as shown in the figure below (the image comes from the network, if there is infringement, please contact to delete)

2. progressive

In the process of gradual image opening, the fuzzy outline of the whole image will be displayed first. With the increase of scanning times, the image will become more and more clear, as shown in the figure below (the image comes from the network, if there is infringement, please contact to delete)

Paste code directly

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

/**
 * Convert picture to streaming
 * @author zhaosx
 *
 */
public class ProgressiveJPEG {

    public static void main(String[] args) throws Exception {
        File file=new File("Z:/2.jpg"); 
        BufferedImage image = ImageIO.read(file); 
        Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("jpeg"); 
        ImageWriter writer=null; 
        while(it.hasNext()) { 
             writer=it.next(); 
             break; 
             //System.out.println(it.next()); 
        } 
        if(writer!=null) { 
             ImageWriteParam params = writer.getDefaultWriteParam(); 
             params.setProgressiveMode(ImageWriteParam.MODE_DEFAULT); 
             //params.setCompressionQuality(0.8f); 
             ImageOutputStream output = ImageIO.createImageOutputStream(new File("Z:/22.jpg")); 
             writer.setOutput(output); 
             writer.write(null,new IIOImage(image,null,null), params); 
             output.flush(); 
             writer.dispose(); 
             System.out.println("ok"); 
        } 

    }

}

Effect comparison:

Using PhotoShop, check "continuous" when saving to save as progressive JPEG

Reference article:
http://blog.jobbole.com/44038/

Topics: network Java