im4java包处理图片

package com.stu.util;  

import java.io.IOException;
import java.util.ArrayList;  

import org.im4java.core.CompositeCmd;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.core.IdentifyCmd;
import org.im4java.process.ArrayListOutputConsumer;  

public class ImagesUtil {
        /**
         * 根据坐标裁剪图片
         *
         * @param srcPath   要裁剪图片的路径
         * @param newPath   裁剪图片后的路径
         * @param x   起始横坐标
         * @param y   起始纵坐标
         * @param x1  结束横坐标
         * @param y1  结束纵坐标
         */
        public static void cutImage(String srcPath, String newPath, int x, int y, int x1,
                int y1)  throws Exception {
            int width = x1 - x;
            int height = y1 - y;
            IMOperation op = new IMOperation();
            op.addImage(srcPath);
            /**  width:裁剪的宽度    * height:裁剪的高度 * x:裁剪的横坐标 * y:裁剪纵坐标  */
            op.crop(width, height, x, y);
            op.addImage(newPath);
            ConvertCmd convert = new ConvertCmd(true);
            convert.run(op);
        }
        /**
         * 根据尺寸缩放图片
         * @param width  缩放后的图片宽度
         * @param height  缩放后的图片高度
         * @param srcPath   源图片路径
         * @param newPath   缩放后图片的路径
         */
        public static void zoomImage(Integer width, Integer height, String srcPath, String newPath) throws Exception {
            IMOperation op = new IMOperation();
            op.addImage(srcPath);
            if(width == null){//根据高度缩放图片
                op.resize(null, height);
            }else if(height == null){//根据宽度缩放图片
                op.resize(width, null);
            }else {
                op.resize(width, height);
            }
            op.addImage(newPath);
            ConvertCmd convert = new ConvertCmd(true);
            convert.run(op);
        }    

        /**
         * 给图片加水印
         * @param srcPath   源图片路径
         */
        public static void addImgText(String srcPath,String content) throws Exception {
            IMOperation op = new IMOperation();
            op.font("微软雅黑");
            op.gravity("southeast");
            op.pointsize(18).fill("#BCBFC8").draw("text 0,0 "+content);   //("x1 x2 x3 x4") x1 格式,x2 x轴距离 x3 y轴距离  x4名称
            op.addImage();
            op.addImage();
            ConvertCmd convert = new ConvertCmd(true);
            try {
              convert.run(op,srcPath,srcPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        /**
         * 图片水印
         *
         * @param srcImagePath   源图片
         * @param waterImagePath 水印
         * @param destImagePath  生成图片
         * @param gravity  图片位置
         * @param dissolve 水印透明度
         */
        public static void waterMark(String waterImagePath, String srcImagePath, String destImagePath, String gravity, int dissolve) {
            IMOperation op = new IMOperation();
            op.gravity(gravity);
            op.dissolve(dissolve);
            op.addImage(waterImagePath);
            op.addImage(srcImagePath);
            op.addImage(destImagePath);
            CompositeCmd cmd = new CompositeCmd(true);
            try {
                cmd.run(op);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IM4JavaException e) {
                e.printStackTrace();
            }
        }
        /**
         * 图片旋转
         *
         * @param srcImagePath
         * @param destImagePath
         * @param angle
         */
        public static void rotate(String srcImagePath, String destImagePath, double angle) {
            try {
                IMOperation op = new IMOperation();
                op.rotate(angle);
                op.addImage(srcImagePath);
                op.addImage(destImagePath);
                ConvertCmd cmd = new ConvertCmd(true);
                cmd.run(op);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }  

        /**
         * 图片信息
         *
         * @param imagePath
         * @return
         */
        public static String showImageInfo(String imagePath) {
            String line = null;
            try {
                IMOperation op = new IMOperation();
                op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");
                op.addImage(1);
                IdentifyCmd identifyCmd = new IdentifyCmd(true);
                ArrayListOutputConsumer output = new ArrayListOutputConsumer();
                identifyCmd.setOutputConsumer(output);
                identifyCmd.run(op, imagePath);
                ArrayList<String> cmdOutput = output.getOutput();
                assert cmdOutput.size() == 1;
                line = cmdOutput.get(0);  

            } catch (Exception e) {
                e.printStackTrace();
            }
            return line;
        }
        /**
         * 图片合成
         * @param args
         * @param maxWidth
         * @param maxHeight
         * @param newpath
         * @param mrg
         * @param type 1:横,2:竖
         */
        public static void montage(String[] args,Integer maxWidth,Integer maxHeight,String newpath,Integer mrg,String type) {
            IMOperation op = new IMOperation();
            ConvertCmd cmd = new ConvertCmd(true);
            String thumb_size = maxWidth+"x"+maxHeight+"^";
            String extent = maxWidth+"x"+maxHeight;
            if("1".equals(type)){
                op.addRawArgs("+append");
            }else if("2".equals(type)){
                op.addRawArgs("-append");
            }  

            op.addRawArgs("-thumbnail",thumb_size);
            op.addRawArgs("-gravity","center");
            op.addRawArgs("-extent",extent);  

            Integer border_w = maxWidth / 40;
            op.addRawArgs("-border",border_w+"x"+border_w);
            op.addRawArgs("-bordercolor","#ccc");  

            op.addRawArgs("-border",1+"x"+1);
            op.addRawArgs("-bordercolor","#fff");  

            for(String img : args){
                op.addImage(img);
            }
            if("1".equals(type)){
                Integer whole_width = ((mrg / 2) +1 + border_w + maxWidth + border_w + (mrg / 2) +1)*args.length - mrg;
                Integer whole_height = maxHeight + border_w + 1;
                op.addRawArgs("-extent",whole_width + "x" +whole_height);
            }else if("2".equals(type)){
                Integer whole_width = maxWidth + border_w + 1;
                Integer whole_height = ((mrg / 2) +1 + border_w + maxHeight + border_w + (mrg / 2) +1)*args.length - mrg;
                op.addRawArgs("-extent",whole_width + "x" +whole_height);
            }
            op.addImage(newpath);
            try {
                cmd.run(op);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }  

        public static void main(String[] args) throws Exception{
          //addImgText("e://a2.jpg");
            //zoomImage(300, 150, "e://a.jpg", "e://a1.jpg");
            //zoomImage(300, 150, "e://b.jpg", "e://b1.jpg");
            //zoomImage(300, 150, "e://c.jpg", "e://c1.jpg");
            //zoomImage(300, 150, "e://d.jpg", "e://d1.jpg");
            //zoomImage(300, 150, "e://e.jpg", "e://e1.jpg");
        //  zoomImage(300, 150, "e://f.jpg", "e://f1.jpg");
             //waterMark("e://cc.jpg", "e://aa.jpg", "e://bb.jpg", "southeast", 30);
            //rotate("e://aa.jpg", "e://ee.jpg", 90);
            //System.out.println(showImageInfo("e://aa.jpg"));
            String[] files = new String[5];
            files[0] = "e://a1.jpg";
            files[1] = "e://b1.jpg";
            files[2] = "e://c1.jpg";
            files[3] = "e://d1.jpg";
            files[4] = "e://e1.jpg";
            //montage(files, 280, 200, "e://liboy1.jpg", 0,"2");
            //cropImage("e://a.jpg", "e://liboy22.jpg", 1024, 727, 500, 350);
            cutImage("e://a.jpg", "e://liboy222.jpg", 5, 10, 100, 120);
        }
} 

原文转自:http://blog.csdn.net/xielibohoudi/article/details/7986908

  1. package com.stu.util;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import org.im4java.core.CompositeCmd;
  5. import org.im4java.core.ConvertCmd;
  6. import org.im4java.core.IM4JavaException;
  7. import org.im4java.core.IMOperation;
  8. import org.im4java.core.IdentifyCmd;
  9. import org.im4java.process.ArrayListOutputConsumer;
  10. public class ImagesUtil {
  11. /**
  12. * 根据坐标裁剪图片
  13. *
  14. * @param srcPath   要裁剪图片的路径
  15. * @param newPath   裁剪图片后的路径
  16. * @param x   起始横坐标
  17. * @param y   起始纵坐标
  18. * @param x1  结束横坐标
  19. * @param y1  结束纵坐标
  20. */
  21. public static void cutImage(String srcPath, String newPath, int x, int y, int x1,
  22. int y1)  throws Exception {
  23. int width = x1 - x;
  24. int height = y1 - y;
  25. IMOperation op = new IMOperation();
  26. op.addImage(srcPath);
  27. /**  width:裁剪的宽度    * height:裁剪的高度 * x:裁剪的横坐标 * y:裁剪纵坐标  */
  28. op.crop(width, height, x, y);
  29. op.addImage(newPath);
  30. ConvertCmd convert = new ConvertCmd(true);
  31. convert.run(op);
  32. }
  33. /**
  34. * 根据尺寸缩放图片
  35. * @param width  缩放后的图片宽度
  36. * @param height  缩放后的图片高度
  37. * @param srcPath   源图片路径
  38. * @param newPath   缩放后图片的路径
  39. */
  40. public static void zoomImage(Integer width, Integer height, String srcPath, String newPath) throws Exception {
  41. IMOperation op = new IMOperation();
  42. op.addImage(srcPath);
  43. if(width == null){//根据高度缩放图片
  44. op.resize(null, height);
  45. }else if(height == null){//根据宽度缩放图片
  46. op.resize(width, null);
  47. }else {
  48. op.resize(width, height);
  49. }
  50. op.addImage(newPath);
  51. ConvertCmd convert = new ConvertCmd(true);
  52. convert.run(op);
  53. }
  54. /**
  55. * 给图片加水印
  56. * @param srcPath   源图片路径
  57. */
  58. public static void addImgText(String srcPath,String content) throws Exception {
  59. IMOperation op = new IMOperation();
  60. op.font("微软雅黑");
  61. op.gravity("southeast");
  62. op.pointsize(18).fill("#BCBFC8").draw("text 0,0 "+content);   //("x1 x2 x3 x4") x1 格式,x2 x轴距离 x3 y轴距离  x4名称
  63. op.addImage();
  64. op.addImage();
  65. ConvertCmd convert = new ConvertCmd(true);
  66. try {
  67. convert.run(op,srcPath,srcPath);
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. /**
  73. * 图片水印
  74. *
  75. * @param srcImagePath   源图片
  76. * @param waterImagePath 水印
  77. * @param destImagePath  生成图片
  78. * @param gravity  图片位置
  79. * @param dissolve 水印透明度
  80. */
  81. public static void waterMark(String waterImagePath, String srcImagePath, String destImagePath, String gravity, int dissolve) {
  82. IMOperation op = new IMOperation();
  83. op.gravity(gravity);
  84. op.dissolve(dissolve);
  85. op.addImage(waterImagePath);
  86. op.addImage(srcImagePath);
  87. op.addImage(destImagePath);
  88. CompositeCmd cmd = new CompositeCmd(true);
  89. try {
  90. cmd.run(op);
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. } catch (InterruptedException e) {
  94. e.printStackTrace();
  95. } catch (IM4JavaException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. /**
  100. * 图片旋转
  101. *
  102. * @param srcImagePath
  103. * @param destImagePath
  104. * @param angle
  105. */
  106. public static void rotate(String srcImagePath, String destImagePath, double angle) {
  107. try {
  108. IMOperation op = new IMOperation();
  109. op.rotate(angle);
  110. op.addImage(srcImagePath);
  111. op.addImage(destImagePath);
  112. ConvertCmd cmd = new ConvertCmd(true);
  113. cmd.run(op);
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. /**
  119. * 图片信息
  120. *
  121. * @param imagePath
  122. * @return
  123. */
  124. public static String showImageInfo(String imagePath) {
  125. String line = null;
  126. try {
  127. IMOperation op = new IMOperation();
  128. op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");
  129. op.addImage(1);
  130. IdentifyCmd identifyCmd = new IdentifyCmd(true);
  131. ArrayListOutputConsumer output = new ArrayListOutputConsumer();
  132. identifyCmd.setOutputConsumer(output);
  133. identifyCmd.run(op, imagePath);
  134. ArrayList<String> cmdOutput = output.getOutput();
  135. assert cmdOutput.size() == 1;
  136. line = cmdOutput.get(0);
  137. } catch (Exception e) {
  138. e.printStackTrace();
  139. }
  140. return line;
  141. }
  142. /**
  143. * 图片合成
  144. * @param args
  145. * @param maxWidth
  146. * @param maxHeight
  147. * @param newpath
  148. * @param mrg
  149. * @param type 1:横,2:竖
  150. */
  151. public static void montage(String[] args,Integer maxWidth,Integer maxHeight,String newpath,Integer mrg,String type) {
  152. IMOperation op = new IMOperation();
  153. ConvertCmd cmd = new ConvertCmd(true);
  154. String thumb_size = maxWidth+"x"+maxHeight+"^";
  155. String extent = maxWidth+"x"+maxHeight;
  156. if("1".equals(type)){
  157. op.addRawArgs("+append");
  158. }else if("2".equals(type)){
  159. op.addRawArgs("-append");
  160. }
  161. op.addRawArgs("-thumbnail",thumb_size);
  162. op.addRawArgs("-gravity","center");
  163. op.addRawArgs("-extent",extent);
  164. Integer border_w = maxWidth / 40;
  165. op.addRawArgs("-border",border_w+"x"+border_w);
  166. op.addRawArgs("-bordercolor","#ccc");
  167. op.addRawArgs("-border",1+"x"+1);
  168. op.addRawArgs("-bordercolor","#fff");
  169. for(String img : args){
  170. op.addImage(img);
  171. }
  172. if("1".equals(type)){
  173. Integer whole_width = ((mrg / 2) +1 + border_w + maxWidth + border_w + (mrg / 2) +1)*args.length - mrg;
  174. Integer whole_height = maxHeight + border_w + 1;
  175. op.addRawArgs("-extent",whole_width + "x" +whole_height);
  176. }else if("2".equals(type)){
  177. Integer whole_width = maxWidth + border_w + 1;
  178. Integer whole_height = ((mrg / 2) +1 + border_w + maxHeight + border_w + (mrg / 2) +1)*args.length - mrg;
  179. op.addRawArgs("-extent",whole_width + "x" +whole_height);
  180. }
  181. op.addImage(newpath);
  182. try {
  183. cmd.run(op);
  184. } catch (Exception e) {
  185. e.printStackTrace();
  186. }
  187. }
  188. public static void main(String[] args) throws Exception{
  189. //addImgText("e://a2.jpg");
  190. //zoomImage(300, 150, "e://a.jpg", "e://a1.jpg");
  191. //zoomImage(300, 150, "e://b.jpg", "e://b1.jpg");
  192. //zoomImage(300, 150, "e://c.jpg", "e://c1.jpg");
  193. //zoomImage(300, 150, "e://d.jpg", "e://d1.jpg");
  194. //zoomImage(300, 150, "e://e.jpg", "e://e1.jpg");
  195. //  zoomImage(300, 150, "e://f.jpg", "e://f1.jpg");
  196. //waterMark("e://cc.jpg", "e://aa.jpg", "e://bb.jpg", "southeast", 30);
  197. //rotate("e://aa.jpg", "e://ee.jpg", 90);
  198. //System.out.println(showImageInfo("e://aa.jpg"));
  199. String[] files = new String[5];
  200. files[0] = "e://a1.jpg";
  201. files[1] = "e://b1.jpg";
  202. files[2] = "e://c1.jpg";
  203. files[3] = "e://d1.jpg";
  204. files[4] = "e://e1.jpg";
  205. //montage(files, 280, 200, "e://liboy1.jpg", 0,"2");
  206. //cropImage("e://a.jpg", "e://liboy22.jpg", 1024, 727, 500, 350);
  207. cutImage("e://a.jpg", "e://liboy222.jpg", 5, 10, 100, 120);
  208. }
  209. }
时间: 2025-01-02 17:11:38

im4java包处理图片的相关文章

GraphicsMagick+Im4Java在windows和linux下的配置

Im4Java包为: im4java-1.2.0.jar 直接在lib下引用即可 GraphicsMagick的安装如下: windows下: 安装:GraphicsMagick-1.3.12-Q16.zip 并配置GraphicsMagick的安装路径 linux下: 安装:GraphicsMagick-1.3.18.tar.gz 1. 解压GraphicsMagick-1.3.12.tar.gz包# tar -xvzf GraphicsMagick-1.3.12.tar.gz# cd Gra

使用tensorflow深度学习识别验证码

除了传统的PIL包处理图片,然后用pytessert+OCR识别意外,还可以使用tessorflow训练来识别验证码. 此篇代码大部分是转载的,只改了很少地方. 代码是运行在linux环境,tessorflow没有支持windows的python 2.7. gen_captcha.py代码. #coding=utf-8 from captcha.image import ImageCaptcha # pip install captcha import numpy as np import ma

Mysql 第六天 使用聚合函数查询

5.1     count()函数 SELECT COUNT(*) FROM t_grade; SELECT COUNT(*) AS total FROM t_grade; SELECT stuName,COUNT(*) FROM t_grade GROUP BY stuName; 5.2     sum函数 SELECT stuName,SUM(score) FROM t_grade WHERE stuName = '张三' SELECT stuName,SUM(score) FROM t_g

django models进行数据库增删查改

引入models的定义 from app.models import  myclass class  myclass(): aa =  models. CharField (max_length=None) bb =  models. CharField (max_length=None) def __unicode__(self): return u'%s %s' %(aa,bb) 增 添加一行数据1 add = myclass(aa='wahaha',bb='hahawa' ) add.sa

网站前端性能优化总结

一.服务器侧优化 1. 添加 Expires 或 Cache-Control 信息头  某些经常使用到.并且不会经常做改动的图片(banner.logo等等).静态文件(登录首页.说明文档等)可以设置较长的有效期(expiration date),这些HTTP头向客户端表明了文档的有效性和持久性.如果有缓存,文档就可以从缓存(除已经过期)而不是从服务器读取.接着,客户端考察缓存中的副本,看看是否过期或者失效,以决定是否必须从服务器获得更新. 各个容器都有针对的方案,,以 Apache 为例: E

Hadoop 2.x HDFS新特性

Hadoop 2.x HDFS新特性     HDFS联邦     HDFS HA(要用到zookeeper等,留在后面再讲)     HDFS快照 回顾: HDFS两层模型     Namespace: 包括目录.文件和块.它支持所有命名空间相关的文件操作,如创建.删除.修改,查看所有文件和目录.     Block Storage Service(块存储服务) 包括两部分: 1 在namenode中的块的管理:提供datanode集群的注册.心跳检测等功能.处理块的报告信息和维护块的位置信息

Eclipse中处理图片引包问题

在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;报错情况下解决方案: Eclipse默认把这些受访问限制的API设成了ERROR.只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden

import com.sun.image.codec.jpeg.JPEGCodec不通过 找不到包(转载)

http://www.xuebuyuan.com/2008608.html 在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;报错:Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required l

import com.sun.image.codec.jpeg.JPEGCodec不通过 找不到包

import com.sun.image.codec.jpeg.JPEGCodec; 在Eclipse中处理图片,需要引入两个包: import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; 报错: Access restriction: The type JPEGImageEncoder is not accessible due to restriction on r