Java automatically generates the thumbnail sample code

  • 2020-04-01 02:09:49
  • OfStack

When you're working on a gallery project, controlling the image size and pixels is the first problem to solve.

One, a single picture generation slightly reduced
The single drawing is redrawn to generate a new picture. The new drawing can be scaled down from the old drawing or its fixed size can be specified.
The detailed code is as follows:


<SPAN style="FONT-SIZE: 14px">import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.util.Map;
public class PicChange {
    /**
     * @param im             The original image 
     * @param resizeTimes    I'm going to scale it down. I'm going to scale it down 2 Times the original 1/2  , the larger the value, the smaller the image returned 
     * @return               Returns the processed image 
     */
    public BufferedImage resizeImage(BufferedImage im, float resizeTimes) {
        
        int width = im.getWidth();
        int height = im.getHeight();
        
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) / resizeTimes);
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) / resizeTimes);
        
        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        return result;
    }
    
    public BufferedImage zoomImage(BufferedImage im, float resizeTimes) {
        
        int width = im.getWidth();
        int height = im.getHeight();
        
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);
        
        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        return result;
    }
    public boolean writeHighQuality(BufferedImage im, String fileFullPath) {
        try {
            
            FileOutputStream newimage = new FileOutputStream(fileFullPath+System.currentTimeMillis()+".jpg");
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
            
            jep.setQuality(1f, true);
            encoder.encode(im, jep);
           
            newimage.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    public static void main(String[] args) throws Exception{
        String inputFoler = "F:\pic" ; 
         
        String outputFolder = "F:\picNew\";  
        
        float times = 0.25f; 
        
        PicChange r = new PicChange();
        File ff = new File("F:\pic\Chrysanthemum1.jpg");
        BufferedImage f = javax.imageio.ImageIO.read(ff);
        r.writeHighQuality(r.zoomImage(f,times), outputFolder);

    }
}</SPAN>

When you move the above code to myEclipse, you may make an error when introducing the toolkit.

<SPAN style="FONT-SIZE: 14px">import com.sun.image.codec.</SPAN>

Solution: simply select the Forbidden references(access rules) in the windows-preferences-java-compiler-errors/scripts as Warning.

Second, batch generation of slightly reduced map
Bulk production of a small thumbnail, which is known in the folder suffix.jpg or other image suffix   After the unified transformation into the other folder has been determined


<SPAN style="FONT-SIZE: 14px">import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.util.Map;
public class ResizeImage {
    /**
     * @param im             The original image 
     * @param resizeTimes    I'm going to scale it down. I'm going to scale it down 2 Times the original 1/2  , the larger the value, the smaller the image returned 
     * @return               Returns the processed image 
     */
    public BufferedImage resizeImage(BufferedImage im, float resizeTimes) {
        
        int width = im.getWidth();
        int height = im.getHeight();
        
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) / resizeTimes);
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) / resizeTimes);
        
        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        return result;
    }
    
    public BufferedImage zoomImage(BufferedImage im, float resizeTimes) {
        
        int width = im.getWidth();
        int height = im.getHeight();
        
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);
        
        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        return result;
    }
    
    public List<BufferedImage> getImageList(String path, String[] type) throws IOException{
        Map<String,Boolean> map = new HashMap<String, Boolean>();
        for(String s : type) {
            map.put(s,true);
        }
        List<BufferedImage> result = new ArrayList<BufferedImage>();
        File[] fileList = new File(path).listFiles();
        for (File f : fileList) {
            if(f.length() == 0)
                continue;
            if(map.get(getExtension(f.getName())) == null)
                continue;
            result.add(javax.imageio.ImageIO.read(f));
        }
        return result;
    }
    /**
     *  Write the picture to disk 
      * @param im
     * @param path     eg: C://home//  The folder address where the image is written to 
      * @param fileName DCM1987.jpg   Write the name of the image 
      * @return
     */
    public boolean writeToDisk(BufferedImage im, String path, String fileName) {
        File f = new File(path + fileName);
        String fileType = getExtension(fileName);
        if (fileType == null)
            return false;
        try {
            ImageIO.write(im, fileType, f);
            im.flush();
            return true;
        } catch (IOException e) {
            return false;
        }
    }
    public boolean writeHighQuality(BufferedImage im, String fileFullPath) {
        try {
            
            FileOutputStream newimage = new FileOutputStream(fileFullPath+System.currentTimeMillis()+".jpg");
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
            
            jep.setQuality(1f, true);
            encoder.encode(im, jep);
           
            newimage.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    public String getExtension(String fileName) {
        try {
            return fileName.split("\.")[fileName.split("\.").length - 1];
        } catch (Exception e) {
            return null;
        }
    }
    public static void main(String[] args) throws Exception{
        String inputFoler = "F:\pic" ; 
         
        String outputFolder = "F:\picNew\";  
        
        float times = 0.25f; 
        
        ResizeImage r = new ResizeImage();
   List<BufferedImage> imageList = r.getImageList(inputFoler,new String[] {"jpg"});
        for(BufferedImage i : imageList) {
         r.writeHighQuality(r.zoomImage(i,times),outputFolder);
  }
    }
}</SPAN>


Related articles: