首页 > Java > java教程 > 正文

Java上传图片后的缩放

大家讲道理
发布: 2016-11-10 10:48:16
原创
1534人浏览过

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
  
import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
  
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
  
public class ImageUtil {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(ImageUtil.class);
  
    public void imageScale(String srcFilePath, String targetFilePath,
            int width, int height) {
        this.imageScale(new File(srcFilePath), new File(targetFilePath), width,
                height);
    }
  
    public void imageScale(File srcFile, File targetFile, int width, int height) {
        try {
            Image image = javax.imageio.ImageIO.read(srcFile);
  
            image = image.getScaledInstance(width, height,
                    Image.SCALE_AREA_AVERAGING);
            // Make a BufferedImage from the Image.
            BufferedImage mBufferedImage = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = mBufferedImage.createGraphics();            
  
            g2.drawImage(image, 0, 0, width, height, Color.white, null);
            g2.dispose();
  
            float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2,
                    -0.125f, -0.125f, -0.125f, -0.125f };
            Kernel kernel = new Kernel(3, 3, kernelData2);
            ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
            mBufferedImage = cOp.filter(mBufferedImage, null);
  
            File targetDir = targetFile.getParentFile();
            if (!targetDir.exists())
                targetDir.mkdirs();
  
            FileOutputStream out = new FileOutputStream(targetFile);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(mBufferedImage);
            out.close();
        } catch (Exception e) {
            logger.error(
                    "imageScale(String, String, int, int) - 图片压缩出错 - srcFilePath="
                            + srcFile.getPath() + ", targeFilePath="
                            + targetFile.getPath() + ", width=" + width
                            + ", height=" + height, e);
        }
    }
  
    public void imageScale(FileItem fileItem, String targetFilePath, int width,
            int height) {
        try {
            InputStream input = fileItem.getInputStream();
            Image image = javax.imageio.ImageIO.read(input);
  
            image = image.getScaledInstance(width, height,
                    Image.SCALE_AREA_AVERAGING);
            BufferedImage mBufferedImage = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = mBufferedImage.createGraphics();
  
            g2.drawImage(image, 0, 0, width, height, Color.white, null);
            g2.dispose();
  
            float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2,
                    -0.125f, -0.125f, -0.125f, -0.125f };
            Kernel kernel = new Kernel(3, 3, kernelData2);
            ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
            mBufferedImage = cOp.filter(mBufferedImage, null);
  
            File target = new File(targetFilePath);
            File targetDir = target.getParentFile();
            if (!targetDir.exists())
                targetDir.mkdirs();
  
            FileOutputStream out = new FileOutputStream(targetFilePath);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(mBufferedImage);
            out.close();
            input.close();
        } catch (Exception e) {
            logger.error(
                    "imageScale(String, String, int, int) - 图片压缩出错 - fileItem="
                            + fileItem.getName() + ", targetFilePath="
                            + targetFilePath + ", width=" + width + ", height="
                            + height, e);
        }
    }
      
    public static void main(String[] args) {
        ImageUtil iu=new ImageUtil();
        iu.imageScale("c:/uploadFiles/191540095661.jpg", "c:/uploadFiles/t.jpg", 270, 220);
    }
}
登录后复制

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号