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

2014-11-24 07:45:48 · 作者: · 浏览: 2
Image scaled = aImage.scaleImage(wideth, height);
146 if(wideth > tl && height > 5){
147 DrawInfo aInfo = new DrawInfo(info);
148 aInfo.setFill(PixelPacket.queryColorDatabase("white"));
149 aInfo.setUnderColor(new PixelPacket(0,0,0,100));
150 aInfo.setPointsize(12);
151 //解决中文乱码问题,自己可以去随意定义个自己喜欢字体,我在这用的微软雅黑
152 String fontPath = "C:/WINDOWS/Fonts/MSYH.TTF";
153 // String fontPath = "/usr/maindata/MSYH.TTF";
154 aInfo.setFont(fontPath);
155 aInfo.setTextAntialias(true);
156 aInfo.setOpacity(0);
157 aInfo.setText(" " + text + "于 " + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " 上传于XXXX网,版权归作者所有!");
158 aInfo.setGeometry("+" + (wideth-tl) + "+" + (height-5));
159 scaled.annotateImage(aInfo);
160 }
161 scaled.setFileName(toImg);
162 scaled.writeImage(info);
163 scaled.destroyImages();
164 }
165
166
167 /**
168 * 切图
169 * @param imgPath 源图路径
170 * @param toPath 修改图路径
171 * @param w
172 * @param h
173 * @param x
174 * @param y
175 * @throws MagickException
176 */
177 public static void cutImg(String imgPath, String toPath, int w, int h, int x, int y) throws MagickException {
178 ImageInfo infoS = null;
179 MagickImage image = null;
180 MagickImage cropped = null;
181 Rectangle rect = null;
182 try {
183 infoS = new ImageInfo(imgPath);
184 image = new MagickImage(infoS);
185 rect = new Rectangle(x, y, w, h);
186 cropped = image.cropImage(rect);
187 cropped.setFileName(toPath);
188 cropped.writeImage(infoS);
189
190 } finally {
191 if (cropped != null) {
192 cropped.destroyImages();
193 }
194 }
195 }
196 }

摘自 Noures.x