หน้าเว็บ

วันอังคารที่ 16 ตุลาคม พ.ศ. 2555

การเขียน java หมุนรูปภาพ (java rotate image file)






package com.blogspot.na5cent.utils;

import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;

/**
 *
 * @author redcrow
 */
public class ImageUtils {

    public static void rotate(File file, int degree) throws IOException {
        if (degree < 0) {
            degree = 360 + degree;
        }

        BufferedImage input = ImageIO.read(file);
        BufferedImage rotated = rotate(input, degree);
        ImageIO.write(rotated, getMimeType(file.getAbsolutePath()), file);
    }

    public static void rotate180(File file) throws IOException {
        BufferedImage input = ImageIO.read(file);
        BufferedImage rotated = rotate(input, 180);
        ImageIO.write(rotated, getMimeType(file.getAbsolutePath()), file);
    }

    public static void rotate90(File file) throws IOException {
        BufferedImage input = ImageIO.read(file);
        BufferedImage rotated = rotate(input, 90);
        ImageIO.write(rotated, getMimeType(file.getAbsolutePath()), file);
    }

    private static String getMimeType(String fileName) {
        if (fileName.toLowerCase().endsWith("png")) {
            return "png";
        } else if (fileName.toLowerCase().endsWith("jpg") || fileName.toLowerCase().endsWith("jpeg")) {
            return "jpeg";
        } else if (fileName.toLowerCase().endsWith("gif")) {
            return "gif";
        } else {
            return "bmp";
        }
    }

    /**
     * I borrow code from
     * http://stackoverflow.com/questions/2687926/how-can-i-rotate-an-image-using-java-swing-and-then-set-its-origin-to-0-0
     * But I found one problem with JPEG encode image. JPEG image has no
     * color,but PNG GIF not found this problem. I discover that if i not
     * specify rendereing hint JPEG encoder perform wrong color on result image.
     * I add this line to fix problem AffineTransformOp op = new
     * AffineTransformOp(xform,new
     * RenderingHints(RenderingHints.KEY_COLOR_RENDERING,
     * RenderingHints.VALUE_COLOR_RENDER_SPEED));
     */
    public static BufferedImage rotate(BufferedImage image, int _theta) {
        /*
         * Affline transform only works with perfect squares. The following
         *   code is used to take any rectangle image and rotate it correctly.
         *   To do this it chooses a center point that is half the greater
         *   length and tricks the library to think the image is a perfect
         *   square, then it does the rotation and tells the library where
         *   to find the correct top left point. The special cases in each
         *   orientation happen when the extra image that doesn't exist is
         *   either on the left or on top of the image being rotated. In
         *   both cases the point is adjusted by the difference in the
         *   longer side and the shorter side to get the point at the 
         *   correct top left corner of the image. NOTE: the x and y
         *   axes also rotate with the image so where width > height
         *   the adjustments always happen on the y axis and where
         *   the height > width the adjustments happen on the x axis.
         *   
         */
        AffineTransform xform = new AffineTransform();
        if (image.getWidth() > image.getHeight()) {
            xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getWidth());
            xform.rotate(Math.toRadians(_theta));

            int diff = image.getWidth() - image.getHeight();

            switch (_theta) {
                case 90:
                    xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff);
                    break;
                case 180:
                    xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff);
                    break;
                default:
                    xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth());
                    break;
            }
        } else if (image.getHeight() > image.getWidth()) {
            xform.setToTranslation(0.5 * image.getHeight(), 0.5 * image.getHeight());
            xform.rotate(Math.toRadians(_theta));

            int diff = image.getHeight() - image.getWidth();

            switch (_theta) {
                case 180:
                    xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight());
                    break;
                case 270:
                    xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight());
                    break;
                default:
                    xform.translate(-0.5 * image.getHeight(), -0.5 * image.getHeight());
                    break;
            }
        } else {
            xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getHeight());
            xform.rotate(Math.toRadians(_theta));
            xform.translate(-0.5 * image.getHeight(), -0.5 * image.getWidth());
        }
        Map<Key, Object> renderingHint = new HashMap<Key, Object>();
        renderingHint.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        renderingHint.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        AffineTransformOp op = new AffineTransformOp(xform, new RenderingHints(renderingHint));
        return op.filter(image, op.createCompatibleDestImage(image, null));
    }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น