Java BufferedImage Zoom zoom(String srcImageFile, String result, int destHeight, int destWidth)

Here you can find the source of zoom(String srcImageFile, String result, int destHeight, int destWidth)

Description

zoom

License

Apache License

Declaration

public static void zoom(String srcImageFile, String result, int destHeight, int destWidth) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.Color;

import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {

    public static void zoom(String srcImageFile, String result, int destHeight, int destWidth) {
        try {//from   w  ww . j  a  va2 s. co m
            BufferedImage srcBufferedImage = ImageIO.read(new File(srcImageFile));
            int imgWidth = destWidth;
            int imgHeight = destHeight;
            int srcWidth = srcBufferedImage.getWidth();
            int srcHeight = srcBufferedImage.getHeight();
            if (srcHeight >= srcWidth) {
                imgWidth = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth));
            } else {
                imgHeight = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight));
            }
            BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = destBufferedImage.createGraphics();
            graphics2D.setBackground(Color.WHITE);
            graphics2D.clearRect(0, 0, destWidth, destHeight);
            graphics2D.drawImage(srcBufferedImage.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH),
                    (destWidth / 2) - (imgWidth / 2), (destHeight / 2) - (imgHeight / 2), null);
            graphics2D.dispose();
            ImageIO.write(destBufferedImage, "JPEG", new File(result));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void zoom(File srcImageFile) {
        try {
            BufferedImage srcBufferedImage = ImageIO.read(srcImageFile);
            int srcWidth = srcBufferedImage.getWidth();
            int srcHeight = srcBufferedImage.getHeight();
            BufferedImage destBufferedImage = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = destBufferedImage.createGraphics();
            graphics2D.setBackground(Color.WHITE);
            graphics2D.clearRect(0, 0, srcWidth, srcHeight);
            graphics2D.drawImage(srcBufferedImage.getScaledInstance(srcWidth, srcHeight, Image.SCALE_SMOOTH),
                    (srcWidth / 2) - (srcWidth / 2), (srcHeight / 2) - (srcHeight / 2), null);
            graphics2D.dispose();
            ImageIO.write(destBufferedImage, "JPEG", new File(srcImageFile.getPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. zoomInImage(BufferedImage originalImage, int width, int height)
  2. zoom(BufferedImage srcBufferedImage, File destFile, int destHeight, int destWidth)