java高清新处理图片

在项目中需要对图片处理(缩小、加水印),一开始用的程序虽然可以解决问题但是缩小后的效果实在是太差了,和PS处理出来的效果差远了,后来找到一种专门处理图片的插件,效果很好,以下是安装过程和代码。
1.首先下载 根据缩略图要求品质的不同下载,进行安装
http://www.imagemagick.org/download/binaries/ImageMagick-6.2.9-4-Q8-windows-dll.exe
http://www.imagemagick.org/download/binaries/ImageMagick-6.2.9-4-Q16-windows-dll.exe
2. 考jmagick.dll 到上面的安装目录
3.将jmagick.jar放在你的%JAVA_HOME%\jre\lib\ext

现在web应用经常会用到缩略图。然后一旦生成的缩略图小于100px   *   100px,一些常用的java包,生成的图片确实有点惨不忍睹。  
现在介绍使用Jmagick的使用,可以生成质量很好的缩略图,当然还有其他很多图像处理的方法。  
首先jmagick(http://www.yeo.id.au/jmagick/)是ImageMagick(http://www.imagemagick.org/)的java   应用的接口。所以要先安装ImageMagick应用程序,你的java应用才能使用,在主页上你可以轻松找到下载的链接。  
1。下载jmagick,imagemagick  
2.安装ImageMagick,网站上有安装方法(windows,unnix),我只在win上做了安装,安装以后把安装目录下所有的dll文件copy到windows/system32/目录下。  
3。下载的jmagick包含jmagick.jar,jmagick.dll文件,jmagick.dll需要copy到windows/system32/目录下。  
4。web应用如果部署到tomcat下,那么最好在catalina.bat文件中改变如下设置  
set   JAVA_OPTS=%JAVA_OPTS%   -Xms256M   -Xmx768M   -XX:MaxPermSize=128M   -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager   -Djava.util.logging.config.file="${catalina.base}\conf\logging.properties"  
避免heap溢出的问题,参数看你自己的机器而定。(   -Xms256M   -Xmx768M   -XX:MaxPermSize=128M   )  
5。还要注意如果部署到web应用,你在使用的class里面需要  
System.setProperty("jmagick.systemclassloader","no");  
要不然会报出UnsatisfiedLinkError:   no   JMagick   in   java.library.path.
import java.awt.Dimension;
import java.awt.Rectangle;
import java.text.SimpleDateFormat;
import java.util.Date;
import magick.CompositeOperator;
import magick.CompressionType;
import magick.DrawInfo;
import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;
import magick.PixelPacket;
import magick.PreviewType;
public class ImageUtils2 {
    static {
        // 不能漏掉这个,不然jmagick.jar的路径找不到
        System.setProperty("jmagick.systemclassloader", "no");
    }
    /**
     * @param width 原图片的宽
     * @param height 原图片的高
     * @return
     */
    private static int[] getWidthHeight(int width, int height, int maxValue) {
        int nWidth = 0;
        int nHeight = 0;
        if (width < height) {
            nWidth = width * maxValue / height;
            nHeight = maxValue;
        } else if (width > height) {
            nHeight = height * maxValue / width;
            nWidth = maxValue;
        } else {
            nWidth = width * maxValue / height;
            nHeight = height * maxValue / width;
        }
        return new int[] { nWidth, nHeight };
    }
    /**
     * 压缩图片
     * 
     * @param filePath 源文件路径
     * @param toPath 缩略图路径
     */
    public static void createImage(String filePath, String toPath, int maxValue) {
        ImageInfo info = null;
        MagickImage image = null;
        Dimension imageDim = null;
        MagickImage scaled = null;
        try {
            info = new ImageInfo(filePath);
            image = new MagickImage(info);
            imageDim = image.getDimension();
            int wideth = imageDim.width;
            int height = imageDim.height;
            int[] wh = getWidthHeight(wideth, height, maxValue);
            wideth = wh[0];
            height = wh[1];
            scaled = image.scaleImage(wideth, height);// 小图片文件的大小.
            scaled.setFileName(toPath);
            scaled.writeImage(info);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (scaled != null) {
                scaled.destroyImages();
            }
        }
    }
    /**
     * 大图片是否处理
     * 
     * @param path
     * @return
     */
    public static boolean isUpdateBigPic(String path) {
        Dimension imageDim = null;
        MagickImage image = null;
        ImageInfo info = null;
        try {
            info = new ImageInfo(path);
            image = new MagickImage(info);
            imageDim = image.getDimension();
            if (imageDim.width > Constants.BPHOTO || imageDim.height > Constants.BPHOTO) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public static void main(String[] args) {
        initShuiToImage2("D:\\test\\sss.jpg", "D:\\test\\sshui.png", 550);
//        System.out.println(isUpdateBigPic("D:\\test\\11.jpg"));
    }
    /**
     * 商品水印
     * 
     * @param filePath 待加水印图片
     * @param shuiPath 水印图片
     * @throws MagickException
     */
    public static void initShuiToImage(String filePath, String shuiPath, int maxValue) {
        ImageInfo info = null;
        MagickImage fImage = null;
        MagickImage sImage = null;
        MagickImage fLogo = null;
        MagickImage sLogo = null;
        Dimension imageDim = null;
        Dimension logoDim = null;
        try {
            info = new ImageInfo();
            fImage = new MagickImage(new ImageInfo(filePath));
            imageDim = fImage.getDimension();
            int width = imageDim.width;
            int height = imageDim.height;
            int[] wh = getWidthHeight(width, height, maxValue);
            width = wh[0];
            height = wh[1];
            sImage = fImage.scaleImage(width, height);
            fLogo = new MagickImage(new ImageInfo(shuiPath));
            logoDim = fLogo.getDimension();
            int lw = logoDim.width;
            int lh = logoDim.height;
            sLogo = fLogo.scaleImage(lw, lh);
            // 水印的位置,宽居中,高于底部5px
            sImage.compositeImage(CompositeOperator.AtopCompositeOp, sLogo, (width - lw) / 2,
                    (height - lh) - 5);
            sImage.setFileName(filePath);
            sImage.writeImage(info);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sImage != null) {
                sImage.destroyImages();
            }
        }
    }
    /**
     * 赠品水印
     * 
     * @param filePath
     * @param shuiPath
     * @param maxValue
     */
    public static void initShuiToImage2(String filePath, String shuiPath, int maxValue) {
        ImageInfo info = null;
        MagickImage fImage = null;
        MagickImage sImage = null;
        MagickImage fLogo = null;
        MagickImage sLogo = null;
        Dimension imageDim = null;
        Dimension logoDim = null;
        try {
            info = new ImageInfo();
            fImage = new MagickImage(new ImageInfo(filePath));
            imageDim = fImage.getDimension();
            int width = imageDim.width;
            int height = imageDim.height;
            int[] wh = getWidthHeight(width, height, maxValue);
            width = wh[0];
            height = wh[1];
            sImage = fImage.scaleImage(width, height);
            fLogo = new MagickImage(new ImageInfo(shuiPath));
            logoDim = fLogo.getDimension();
            int lw = logoDim.width;
            int lh = logoDim.height;
            sLogo = fLogo.scaleImage(lw, lh);
            // 水印的位置,宽居中,高于底部
            sImage.compositeImage(CompositeOperator.AtopCompositeOp, sLogo, 0,
                    0);
            sImage.setFileName(filePath);
            sImage.writeImage(info);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sImage != null) {
                sImage.destroyImages();
            }
        }
    }
    /**
     * 水印(文字)
     * 
     * @param filePath 源文件路径
     * @param toImg 修改图路径
     * @param text 名字(文字内容自己随意)
     * @throws MagickException
     */
    public static void initTextToImg(String filePath, String toImg, String text) throws MagickException {
        ImageInfo info = new ImageInfo(filePath);
        if (filePath.toUpperCase().endsWith("JPG") || filePath.toUpperCase().endsWith("JPEG")) {
            info.setCompression(CompressionType.JPEGCompression); // 压缩类别为JPEG格式
            info.setPreviewType(PreviewType.JPEGPreview); // 预览格式为JPEG格式
            info.setQuality(95);
        }
        MagickImage aImage = new MagickImage(info);
        Dimension imageDim = aImage.getDimension();
        int wideth = imageDim.width;
        int height = imageDim.height;
        if (wideth > 660) {
            height = 660 * height / wideth;
            wideth = 660;
        }
        int a = 0;
        int b = 0;
        String[] as = text.split("");
        for (String string : as) {
            if (string.matches("[\u4E00-\u9FA5]")) {
                a++;
            }
            if (string.matches("[a-zA-Z0-9]")) {
                b++;
            }
        }
        int tl = a * 12 + b * 6 + 300;
        MagickImage scaled = aImage.scaleImage(wideth, height);
        if (wideth > tl && height > 5) {
            DrawInfo aInfo = new DrawInfo(info);
            aInfo.setFill(PixelPacket.queryColorDatabase("white"));
            aInfo.setUnderColor(new PixelPacket(0, 0, 0, 100));
            aInfo.setPointsize(12);
            // 解决中文乱码问题,自己可以去随意定义个自己喜欢字体,我在这用的微软雅黑
            String fontPath = "C:/WINDOWS/Fonts/MSYH.TTF";
            // String fontPath = "/usr/maindata/MSYH.TTF";
            aInfo.setFont(fontPath);
            aInfo.setTextAntialias(true);
            aInfo.setOpacity(0);
            aInfo.setText(" " + text + "于 " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())
                    + " 上传于XXXX网,版权归作者所有!");
            aInfo.setGeometry("+" + (wideth - tl) + "+" + (height - 5));
            scaled.annotateImage(aInfo);
        }
        scaled.setFileName(toImg);
        scaled.writeImage(info);
        scaled.destroyImages();
    }
    /**
     * 切图
     * 
     * @param imgPath 源图路径
     * @param toPath 修改图路径
     * @param w
     * @param h
     * @param x
     * @param y
     * @throws MagickException
     */
    public static void cutImg(String imgPath, String toPath, int w, int h, int x, int y)
            throws MagickException {
        ImageInfo infoS = null;
        MagickImage image = null;
        MagickImage cropped = null;
        Rectangle rect = null;
        try {
            infoS = new ImageInfo(imgPath);
            image = new MagickImage(infoS);
            rect = new Rectangle(x, y, w, h);
            cropped = image.cropImage(rect);
            cropped.setFileName(toPath);
            cropped.writeImage(infoS);
        } finally {
            if (cropped != null) {
                cropped.destroyImages();
            }
        }
    }
    /**
     * 图片翻转
     * 
     * @param filePath
     * @param jaodu
     * @throws MagickException
     */
    public static void createFanImg(String filePath, Double jaodu) throws MagickException {
        ImageInfo info = null;
        MagickImage image = null;
        try {
            info = new ImageInfo(filePath);
            image = new MagickImage(info);
            MagickImage rotated = image.rotateImage(jaodu);
            rotated.setFileName(filePath);
            rotated.writeImage(info);
        } finally {
            if (image != null) {
                image.destroyImages();
            }
        }
    }
}

你可能感兴趣的:(java,tomcat,Web,windows,ext)