java应用ImageMagick处理比较大的图片 (二)

2014-11-24 07:45:48 · 作者: · 浏览: 3
scaled.setFileName(toPath);
61 scaled.writeImage(info);
62 }finally{
63 if(scaled != null){
64 scaled.destroyImages();
65 }
66 }
67 }
68
69 /**
70 * 水印(图片logo)
71 * @param filePath 源文件路径
72 * @param toImg 修改图路径
73 * @param logoPath logo图路径
74 * @throws MagickException
75 */
76 public static void initLogoImg(String filePath, String toImg, String logoPath) throws MagickException {
77 ImageInfo info = new ImageInfo();
78 MagickImage fImage = null;
79 MagickImage sImage = null;
80 MagickImage fLogo = null;
81 MagickImage sLogo = null;
82 Dimension imageDim = null;
83 Dimension logoDim = null;
84 try {
85 fImage = new MagickImage(new ImageInfo(filePath));
86 imageDim = fImage.getDimension();
87 int width = imageDim.width;
88 int height = imageDim.height;
89 if (width > 660) {
90 height = 660 * height / width;
91 width = 660;
92 }
93 sImage = fImage.scaleImage(width, height);
94
95 fLogo = new MagickImage(new ImageInfo(logoPath));
96 logoDim = fLogo.getDimension();
97 int lw = width / 8;
98 int lh = logoDim.height * lw / logoDim.width;
99 sLogo = fLogo.scaleImage(lw, lh);
100
101 sImage.compositeImage(CompositeOperator.AtopCompositeOp, sLogo, width-(lw + lh/10), height-(lh + lh/10));
102 sImage.setFileName(toImg);
103 sImage.writeImage(info);
104 } finally {
105 if(sImage != null){
106 sImage.destroyImages();
107 }
108 }
109 }
110
111 /**
112 * 水印(文字)
113  * @param filePath 源文件路径
114 * @param toImg 修改图路径
115 * @param text 名字(文字内容自己随意)
116 * @throws MagickException
117 */
118 public static void initTextToImg(String filePath, String toImg, String text) throws MagickException{
119 ImageInfo info = new ImageInfo(filePath);
120 if (filePath.toUpperCase().endsWith("JPG") || filePath.toUpperCase().endsWith("JPEG")) {
121 info.setCompression(CompressionType.JPEGCompression); //压缩类别为JPEG格式
122 info.setPreviewType(PreviewType.JPEGPreview); //预览格式为JPEG格式
123 info.setQuality(95);
124 }
125 MagickImage aImage = new MagickImage(info);
126 Dimension imageDim = aImage.getDimension();
127 int wideth = imageDim.width;
128 int height = imageDim.height;
129 if (wideth > 660) {
130 height = 660 * height / wideth;
131 wideth = 660;
132 }
133 int a = 0;
134 int b = 0;
135 String[] as = text.split("");
136 for (String string : as) {
137 if(string.matches("[\u4E00-\u9FA5]")){
138 a++;
139 }
140 if(string.matches("[a-zA-Z0-9]")){
141 b++;
142 }
143 }
144 int tl = a*12 + b*6 + 300;
145 Magick