在做pdf文檔轉成jpg的時候,發現了Jmagick的創建高質量的圖片的一個java類庫,自己以前使用另外的一個類庫,感覺這個更好點,就試着用了下,感覺不錯。
1.使用的windows下的jmagick-win-6.3.9-Q16.zip 地址是: http://downloads.jmagick.org/6.3.9/ 2.doc對應的api地址:http://downloads.jmagick.org/jmagick-doc/ 3.安裝ImageMagick,官方網站:http://www.imagemagick.org/ 我使用的是:ImageMagick-6.4.6-4-Q16-windows-dll.exe :點擊下載 4. 安裝ImageMagick-6.4.6-4-Q16-windows-dll.exe,將 安裝目錄下(按自己所安裝的目錄找) 下的所有dll文件 copy 到系統盤下的 “C:\WINDOWS\system32\”文件夾裏 5. 配置環境變量 6.解壓jmagick-win-6.3.9-Q16.zip 7.web應用如果部署到tomcat下,那麼最好在catalina.bat文件中改變如下設置 8.還要注意如果部署到web應用,你在使用的class裏面需要
再環境變量path裏添加新的值 “C:\Program Files\ImageMagick-6.4.6-4-Q16“使用IDE可以不用配置
將 jmagick.dll 複製到系統盤下的 “C:\WINDOWS\system32\”文件夾裏 和 複製到jdk的bin(例“D:\jdk6\bin”)文件裏各一份
將 jmagick.jar 複製到Tomcat下的lib文件夾裏 和 所使用項目的WEB-INF下lib文件裏各一份;還要將jmagick.jar複製到
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 )
System.setProperty(“jmagick.systemclassloader”,”no”);
要不然會報出UnsatisfiedLinkError: no JMagick in java.library.path.
實例測試code:
常用的水印,切圖,壓縮等簡單程序工具類,繼續下面
- package com.utils;
- 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 Aa {
- static{
- //不能漏掉這個,不然jmagick.jar的路徑找不到
- System.setProperty("jmagick.systemclassloader","no");
- }
- /**
- * 壓縮圖片
- * @param filePath 源文件路徑
- * @param toPath 縮略圖路徑
- */
- public static void createThumbnail(String filePath, String toPath) throws MagickException{
- 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;
- if (wideth > height) {
- height = 660 * height / wideth;
- wideth = 660;
- }
- scaled = image.scaleImage(wideth, height);//小圖片文件的大小.
- scaled.setFileName(toPath);
- scaled.writeImage(info);
- }finally{
- if(scaled != null){
- scaled.destroyImages();
- }
- }
- }
- /**
- * 水印(圖片logo)
- * @param filePath 源文件路徑
- * @param toImg 修改圖路徑
- * @param logoPath logo圖路徑
- * @throws MagickException
- */
- public static void initLogoImg(String filePath, String toImg, String logoPath) throws MagickException {
- ImageInfo info = new ImageInfo();
- MagickImage fImage = null;
- MagickImage sImage = null;
- MagickImage fLogo = null;
- MagickImage sLogo = null;
- Dimension imageDim = null;
- Dimension logoDim = null;
- try {
- fImage = new MagickImage(new ImageInfo(filePath));
- imageDim = fImage.getDimension();
- int width = imageDim.width;
- int height = imageDim.height;
- if (width > 660) {
- height = 660 * height / width;
- width = 660;
- }
- sImage = fImage.scaleImage(width, height);
- fLogo = new MagickImage(new ImageInfo(logoPath));
- logoDim = fLogo.getDimension();
- int lw = width / 8;
- int lh = logoDim.height * lw / logoDim.width;
- sLogo = fLogo.scaleImage(lw, lh);
- sImage.compositeImage(CompositeOperator.AtopCompositeOp, sLogo, width-(lw + lh/10), height-(lh + lh/10));
- sImage.setFileName(toImg);
- sImage.writeImage(info);
- } 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();
- }
- }
- }
- }