mylzdy的gravatar头像
mylzdy 2015-09-13 09:47:09

java调用ImageMagick为什么抛出magick.MagickException: Unable to retrieve handle的异常?

下载地址:http://www.jmagick.org/6.3.9/
我的系统是windowsXp的,所以装的是windows版本的
版本号:ImageMagick-6.3.9-0-Q16-windows-dll.exe
jar:    jmagick-win-6.3.9-Q16.zip
以上的应用和jar包和dll文件都在下载路径下。

以下是程序

TestCasePictureThumbnail.java

package com.wocloud.testcase;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.funambol.foundation.util.MediaUtils;
import com.funambol.foundation.util.TestImageIO;
import com.funambol.foundation.util.TestImageMagick;


public class TestCasePictureThumbnail extends BaseTestCase {
    
     
    @test
    public void test_1_picture_thumbnail() throws Exception {  
        File[] mediaHubs = new File[] {
                new File("c:/testcase-sync-download-pictures/"),
                new File("c:/testcase-backup-download-pictures/") };
        
        double tolerance = 0;
        File thumbFile;
        int[] thumbs = new int[] { 40, 80, 200, 140, 130, 120, 110 };
        String imageName;
        for (File mediaHub : mediaHubs) {
            
            for (File imageFile : mediaHub.listFiles()) {
        
                imageName = imageFile.getName();
                
                for (int thumb : thumbs) {
                    thumbFile = new File("c:\\testcase-picture-thumbnail\\" + imageName + "_" + thumb+"x"+thumb+".gif" + getExtendName(imageName));
                    int thumbY = thumb;
                    int thumbX = thumb;
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    InputStream is = new FileInputStream(imageFile);
                    byte[] bytes = new byte[1024 * 512];//1024 * 512
                    int reads = -1;
                    while ((reads = is.read(bytes)) > 0) {
                        
                        baos.write(bytes, 0, reads);
                    }
                    is.close();
                    is = null;
                    //baos.toByteArray()
                    if(!thumbFile.getParentFile().exists()){
                        thumbFile.getParentFile().mkdir();
                    }
                    TestImageMagick.createPictureThumbnails(baos.toByteArray(), thumbFile, thumbX, thumbY, imageName, tolerance);
                }
            }
            
        }
        
        System.out.println("Ok!");
    }
    
    private String getExtendName(String fileName){
        return fileName.substring(fileName.lastIndexOf("."));
    }
}

TestImageMagick.java

package com.funambol.foundation.util;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifDirectory;

public class TestImageMagick {
    static{   
        //不能漏掉这个,不然jmagick.jar的路径找不到   
        System.setProperty("jmagick.systemclassloader","no");   
    }      
    public static void main(String[] args) {
        

    }
     public static boolean createPictureThumbnails(byte[] in,
                File thumbFile,
                int thumbX,
                int thumbY,
                String imageName,
                double tolerance) throws IOException, MagickException {

        
         /**
             * 角度
             *         0:     正常
             *         90:   需要按顺时针旋转90度
             *         180: 需要按顺时针旋转180度
             *         270: 需要按顺时针旋转270度
             */
            String thumbFilePath=thumbFile.getAbsolutePath();
            MagickImage mi=null;
            FileOutputStream fileOutputStream  = null;
            MagickImage image=null;        
            File newFile=null;
            Dimension imageDim=null;
            
            try{
            long beginTime=System.currentTimeMillis();
             // Determines thumbnail height, width and quality缩略图尺寸
            newFile=new File("d:\\pic\\"+imageName);
            fileOutputStream=new FileOutputStream(newFile);
            fileOutputStream.write(in, 0, in.length);
            long endTime=System.currentTimeMillis();
            System.out.println(endTime-beginTime);
         int thumbWidth = thumbX;
         int thumbHeight = thumbY;
        /* Thread.sleep(3000);*/
         Thread.sleep(3000);
         
         String filepath=newFile.getAbsolutePath();
         ImageInfo info=new ImageInfo(filepath);
         image=new MagickImage(new ImageInfo(filepath));
         imageDim=image.getDimension();
         mi=image.scaleImage(thumbWidth, thumbHeight);   
         mi.setFileName(thumbFilePath);   
         mi.writeImage(info);   
           
             //第三压缩图片
             //接着处理图片的格式
             //最后输出
             
         }catch(Exception e){
             e.printStackTrace();
         } finally {
             if(mi!=null){
                 mi.destroyImages();
             }
             if(image!=null){
                 image.destroyImages();
             }
             //关闭流
             
             if (fileOutputStream!=null) {
                 fileOutputStream.close();
             }
             fileOutputStream = null;
         }
            
            
     
         return true;
    
     }
     public static boolean createPictureThumbnail(byte[] in,
                File thumbFile,
                int thumbX,
                int thumbY,
                String imageName,
                double tolerance) throws IOException, MagickException {

        
         /**
             * 角度
             *         0:     正常
             *         90:   需要按顺时针旋转90度
             *         180: 需要按顺时针旋转180度
             *         270: 需要按顺时针旋转270度
             */
            String thumbFilePath=thumbFile.getAbsolutePath();
            MagickImage mi=null;
            Dimension imageDim=null;
            FileOutputStream fileOutputStream  = null;
            MagickImage image=null;
            Rectangle rect = null;
            String ori = null;
            File newFile=null;
            try {
                 ori = getOrientationDescription(new ByteArrayInputStream(in));
            } catch (MetadataException e) {
                ori = "-1";            
            } catch (JpegProcessingException e) {
                ori = "-1";
            }catch(Exception ex){
                ori = "-1";
            }
            try{
            long beginTime=System.currentTimeMillis();
             // Determines thumbnail height, width and quality缩略图尺寸
            newFile=new File("d:\\pic\\"+imageName);
            fileOutputStream=new FileOutputStream(newFile);
            fileOutputStream.write(in, 0, in.length);
            long endTime=System.currentTimeMillis();
            System.out.println(endTime-beginTime);
            int thumbWidth = thumbX;
            int thumbHeight = thumbY;
           /* Thread.sleep(3000);*/
            Thread.sleep(1000);
            
            String filepath=newFile.getAbsolutePath();
            ImageInfo info=new ImageInfo(filepath);
            image=new MagickImage(new ImageInfo(filepath));
            /*Thread.sleep(3000);*/            
            imageDim=image.getDimension();
            int imageWidth=imageDim.width;
            int imageHeight=imageDim.height;
            //判断宽高是否相等,先切图
            if(imageWidth!=imageHeight){
                //判断请求参数图片格式是否是正方形
                if(thumbWidth==thumbHeight){
                     if (imageWidth>imageHeight) {
                         rect = new Rectangle(0, 0, imageHeight, imageHeight);//(imageWidth-imageHeight)/2
                     }else {
                         rect = new Rectangle(0, 0, imageWidth, imageWidth);//(imageHeight-imageWidth)/2
                     }
                     image=image.cropImage(rect);//创建新图片
                     image.setFileName(thumbFilePath);
                     image.writeImage(info);
                     imageWidth=image.getDimension().width;
                     imageHeight=image.getDimension().height;
                }
            }
            
            //再等比缩图
             double imageRatio = (double) imageWidth / (double) imageHeight;
             //等比缩放(高为0)
             if (thumbHeight==0) {
                  thumbHeight = (int) (thumbWidth / imageRatio);
                }
             //等比缩放(宽为0)
             if (thumbWidth==0) {
                 thumbWidth = (int) (thumbHeight * imageRatio);
                }
             double thumbRatio = (double) thumbWidth / (double) thumbHeight;
             // Don't create the thumbnail if the original file is smaller
             // than required size increased by % tolerance
             if (imageWidth <= (thumbWidth * (1 + tolerance / 100))
                 && imageHeight <= (thumbHeight * (1 + tolerance / 100))) {
                 
                 return false;
             }
             if (thumbRatio < imageRatio) {
                 thumbHeight = (int) (thumbWidth / imageRatio);
             } else {
                 thumbWidth = (int) (thumbHeight * imageRatio);
             }
             //图片缩放,生成缩略图
             mi=image.scaleImage(thumbWidth, thumbHeight);
             mi.setFileName(thumbFilePath);
             mi.writeImage(info);
              
                //第三压缩图片
                //接着处理图片的格式
                //最后输出
                
            }catch(Exception e){
                e.printStackTrace();
            } finally {
                if(mi!=null){
                    mi.destroyImages();
                }
                if(image!=null){
                    image.destroyImages();
                }
                //关闭流
                
                if (fileOutputStream!=null) {
                    fileOutputStream.close();
                }
                fileOutputStream = null;
            }
            
            
        
         return true;
    
     }
    
        /**
         * 获取图片流的旋转角度
         * @param in
         * @return
         * @throws MetadataException
         * @throws JpegProcessingException
         *   关于镜面翻转暂时不考虑了
         */
        public static String getOrientationDescription(InputStream in) throws MetadataException, JpegProcessingException{    
            Metadata metadata = JpegMetadataReader.readMetadata(in);  
            Directory _directory = metadata.getDirectory(ExifDirectory.class);  
            if (!_directory.containsTag(ExifDirectory.TAG_ORIENTATION)) return null;  
            int orientation = _directory.getInt(ExifDirectory.TAG_ORIENTATION);  
            switch (orientation) {  
                case 1:
                        //return "Top, left side (Horizontal / normal)";  //0
                        return "0";  //0
                case 2:
                        //return "Top, right side (Mirror horizontal)";
                        return "-1";
                case 3:
                        //return "Bottom, right side (Rotate 180)";  //180
                        return "180";  //180
                case 4:
                        //return "Bottom, left side (Mirror vertical)";  
                        return "-1";  
                case 5:
                        //return "Left side, top (Mirror horizontal and rotate 270 CW)";  
                        return "-1";  
                case 6:
                        //return "Right side, top (Rotate 90 CW)";  //90
                        return "90";  //90
                case 7:
                        //return "Right side, bottom (Mirror horizontal and rotate 90 CW)";  
                        return "-1";  
                case 8:
                        //return "Left side, bottom (Rotate 270 CW)";  //270
                        return "270";  //270
                default:  
                        //return String.valueOf(orientation);
                        return "-1";  
            }  
        }  

}
所有回答列表(1)
mylzdy的gravatar头像
mylzdy  LV12 2015年9月15日

问题已解决,仔细看了下ImageMagickAPI,问题解决了,心情好爽。

评论(0) 最佳答案
相关问答
最近浏览
z824057572  LV4 2020年3月3日
jFreeChart  LV25 2018年6月11日
xiaopq  LV2 2018年5月2日
nice2017 2018年1月15日
暂无贡献等级
樱花满天  LV24 2016年2月23日
tuchief  LV3 2015年9月16日
浪子逍遥遥  LV18 2015年9月14日
sadma0001  LV3 2015年9月14日
Watson  LV13 2015年9月14日
touch39  LV10 2015年9月14日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友