日期:2014-05-17  浏览次数:20771 次

图片不变形缩放裁剪
最近做一个项目用到图片的上传操作,怎么实现图片的上传缩放裁剪后图片不变形。例如将图片经过处理后变成统一的高宽比例而图片不变形。请各位高手指教。

------解决方案--------------------
比例缩小,不足部分补上透明像素,最简单的办法!
------解决方案--------------------
使用drawimage将图片缩放裁剪到合适比例 在使用imagewriter将图片保存到本地就行了 或者保存到服务器
------解决方案--------------------
明天上班给你发代码,各种变形、剪裁!mark了
------解决方案--------------------
求共享,学习了
探讨

明天上班给你发代码,各种变形、剪裁!mark了

------解决方案--------------------
Java code

package com.bean.tuil;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
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.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import javax.imageio.ImageIO;

/**
 * 对图片的操作
 * 
 * @author yangyz
 * 
 */
public class ImageControl {
    // 添加字体的属性值
    private Font font = new Font("", Font.PLAIN, 12);

    private Graphics2D g = null;

    private int fontsize = 0;

    private int x = 0;

    private int y = 0;

    private String destDir = "F:\\";

    public void setDestDir(String path) {
        this.destDir = path;
        // 目录不存在
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    /**
     * 导入本地图片到缓冲中
     * 
     * @param imgPath
     * @return
     */
    public BufferedImage loadImageLocal(String imgPath) {
        try {
            return ImageIO.read(new File(imgPath));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 读取网络中的图片到缓冲
     * 
     * @param imgPath
     * @return
     */
    public BufferedImage loadImageUrl(String imgPath) {
        URL url;
        try {
            url = new URL(imgPath);
            return ImageIO.read(url);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 生成新图片到本地
     * 
     * @param newImage
     *            新名字
     * @param img
     *            缓冲
     * @param type
     *            类别
     */
    public void writeImageLocal(BufferedImage img, String type) {
        if (img != null) {
            File outputFile = new File(this.destDir + new Date().getTime()
                    + "." + type);
            try {
                ImageIO.write(img, type, outputFile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 设置文字字体,大小
     * 
     * @param fontStyle
     * @param fontSize
     */
    public void setFont(String fontStyle, int fontSize) {
        this.fontsize = fontSize;
        this.font = new Font(fontStyle, Font.PLAIN, fontSize);
    }

    /**
     * 单行文本
     * 
     * @param img
     * @param content
     * @param x
     * @param y
     * @return
     */
    public BufferedImage modifyImage(BufferedImage img, Object content) {
        try {
            int w = img.getWidth();
            int h = img.getHeight();

            g = img.createGraphics();
            g.setColor(Color.RED);
            if (