设为首页 加入收藏

TOP

Linux平台下对JMagick的一个简单封装(二)
2014-11-24 03:03:29 来源: 作者: 【 】 浏览:5
Tags:Linux 台下 JMagick 一个 简单 封装
ina.com 2010-8-11
*/
public static void createWaterPrintByText(String filePath, String toImg, String text
, Point pos, int pointSize)
throws MagickException {
if (!checkType(filePath) || !checkType(toImg)) {
return;
}

if (null == text || "".equals(text)) {
text = "sulliy@sina.com";
}

ImageInfo info = new ImageInfo(filePath);
if (filePath.toUpperCase().endsWith("JPG")
|| filePath.toUpperCase().endsWith("JPEG")) {
info.setCompression(CompressionType.JPEGCompression); // 压缩类别为JPEG格式
info.setPreviewType(PreviewType.JPEGPreview); // 预览格式为JPEG格式
info.setQuality(95);
}
MagickImage aImage = new MagickImage(info);
Dimension imageDim = aImage.getDimension();
int width = imageDim.width;
int height = imageDim.height;

if (width <= (int)pos.getX() || height <= (int)pos.getY()) {
pos.setLocation(0, 0);
}

int a = 0;
int b = 0;
String[] as = text.split("");
for (String string : as) {
if (string.matches("[\u4E00-\u9FA5]")) {
a++;
}
if (string.matches("[a-zA-Z0-9]")) {
b++;
}
}
int tl = a * 12 + b * 6 ;//字符长度
MagickImage scaled = aImage.scaleImage(width, height);
if (width > tl && height > 5) {
DrawInfo aInfo = new DrawInfo(info);
aInfo.setFill(PixelPacket.queryColorDatabase("white"));
aInfo.setUnderColor(new PixelPacket(65535, 65535, 65535, 65535));//设置为透明颜色
aInfo.setPointsize(pointSize);
// 解决中文乱码问题,自己可以去随意定义个自己喜欢字体,我在这用的微软雅黑
String fontPath = "C:/WINDOWS/Fonts/MSIMHEI.TTF";
// String fontPath = "/usr/maindata/MSYH.TTF";
aInfo.setFont(fontPath);
aInfo.setTextAntialias(true);
aInfo.setOpacity(0);//透明度
aInfo.setText(text);
aInfo.setGeometry("+" + ((int)pos.getX() + "+" + (int)pos.getY()));
scaled.annotateImage(aInfo);
}
scaled.setFileName(toImg);
scaled.writeImage(info);
scaled.destroyImages();
}


/**切取图片
* @param imgPath 原图路径
* toPath 生成文件位置
* w 左上位置横坐标
* h 左上位置竖坐标
* x 右下位置横坐标
* y 右下位置竖坐标
* @return
* @throw MagickException
* @author sulliy@sina.com 2010-8-11
*/
public static void cutImg(String imgPath, String toPath, int w, int h,
int x, int y) throws MagickException {
ImageInfo infoS = null;
MagickImage image = null;
MagickImage cropped = null;
Rectangle rect = null;
try {
infoS = new ImageInfo(imgPath);
image = new MagickImage(infoS);
rect = new Rectangle(x, y, w, h);
cropped = image.cropImage(rect);
cropped.setFileName(toPath);
cropped.writeImage(infoS);


} finally {
if (cropped != null) {
cropped.destroyImages();
}
}
}

/**删除图片文件
* @param src 图片位置
* @return
* @throw
* @author sulliy@sina.com 2010-8-11
*/
public static boolean removeFile(String src) throws SecurityException{
try {
if (!checkType(src)) {
return false;
}

File file = new File(src);
return file.delete();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}


可以写个简单的测试


import java.awt.Point;


import magick.MagickException;



public class JMagicjWrapperTest {
public static void main(String[] args) throws MagickException{

//test for function imageResize

JMagicjWrapper.imageResize("pics.jpg", "reSize20x30.png", 20, 30);
JMagicjWrapper.imageResize("pics.jpg", "reSize250x200.jpeg", 250, 200);
JMagicjWrapper.imageResize("pics.jpg", "reSize50x50.jpg", 50, 50);
JMagicjWrapper.imageResize("pics.jp

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Ubuntu 10.04 编译源码运行模拟器 下一篇Linux 2.6.32 在Arm9(s3c2440)..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Announcing October (2025-12-24 15:18:16)
·MySQL有什么推荐的学 (2025-12-24 15:18:13)
·到底应该用MySQL还是 (2025-12-24 15:18:11)
·进入Linux世界大门的 (2025-12-24 14:51:47)
·Download Linux | Li (2025-12-24 14:51:44)