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"; } } }
评论
- 等 最代码怎么获取牛币啊?
- 完 谁来告诉我最代码上线的时间,答对者给5牛币,先来先得
- 等 牛友们,大家好,你们做程序员多久了?现在还好吗?
- 完 在微信打开的页面里进行app下载
- 等 最代码2014年欢乐聚声会
- 完 mysql如何查询表数据并且对3个字段降序的SQL?
- 完 最代码牛币机制改革
- 完 成功的在bae上使用了自定义运行环境 jetty+nginx的组合,大家对jetty+nginx优化有哪些心得?
- 完 进来分享一下各位牛牛是如何加入最代码大家庭的?
- 等 为什么java BufferedImage类处理大图直接抛出内存溢出的异常?
- 等 最代码是否开发手机app客户端?
- 完 java程序员学习哪些java的技术?java有哪些框架?都能做哪方面的开发?
- 等 php格式网页文件怎么运行?
- 等 Java volatile值获取的问题
- 等 前端vue,拦截了登录后台后,返回的token,requests拦截token,但是发送请求的时候,就出现跨越异常
- 等 大专本科计算机科班怎么找到Java工作?
- 等 eclipse怎么把三个java swing游戏项目合成一个项目?
- 完 伙伴们,大家都有什么好的解压方式么,分享一下~
- 完 三四线城市,6、7k,运维工作,索然无味,想去辞职上培训,各位牛牛有什么建议嘛
- 等 jsp页面输入中文变成问号
- 等 JPA在线上运行一段时间后报错Caused by: java.lang.IncompatibleClassChangeError: null
- 等 PHP 这个规则用preg_match_all怎么写
- 等 大佬们,有没有知道Alfresco如何配置LDAP登录呢?
- 等 php的install目录是框架带的吗?


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日