zip Image File - Java 2D Graphics

Java examples for 2D Graphics:Image File

Description

zip Image File

Demo Code


//package com.java2s;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    public static String zipImageFile(String oldFile, int width,
            int height, float quality, String smallIcon) {
        if (oldFile == null) {
            return null;
        }/*from w w w  . j  av  a 2s. co m*/
        String newImage = null;
        try {
            /** ? */
            Image srcFile = ImageIO.read(new File(oldFile));
            /** ?,? */
            BufferedImage tag = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
            String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
            /** ? */
            newImage = filePrex + smallIcon
                    + oldFile.substring(filePrex.length());
            /** ? */
            ImageIO.write(tag, filePrex, new File(newImage));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newImage;
    }
}

Related Tutorials