jmagick 对图片处理
package sun.image.jmagick;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.text.SimpleDateFormat;
import java.util.Date;
import magick.*;
public class ImageHandle {
static{
//不能漏掉这个,不然jmagick.jar的路径找不到
System.setProperty("jmagick.systemclassloader","no");
}
/**
* 压缩图片
* @param filePath 源文件路径
* @param toPath 缩略图路径
*/
public static void createThumbnail(String filePath, String toPath) throws MagickException{
ImageInfo info = null;
MagickImage image = null;
Dimension imageDim = null;
MagickImage scaled = null;
try{
info = new ImageInfo(filePath);
image = new MagickImage(info);
imageDim = image.getDimension();
int wideth = imageDim.width;
int height = imageDim.height;
if (wideth > height) {
height = 660 * height / wideth;
wideth = 660;
}
scaled = image.scaleImage(wideth, height);//小图片文件的大小.
scaled.setFileName(toPath);
scaled.writeImage(info);
}finally{
if(scaled != null){
scaled.destroyImages();
}
}
}
/**
* 水印(图片logo)
* @param filePath 源文件路径
* @param toImg 修改图路径
* @param logoPath logo图路径
* @throws MagickException
*/
public static void initLogoImg(String filePath, String toImg, String logoPath) throws MagickException {
ImageInfo info = new ImageInfo();
MagickImage fImage = null;
MagickImage sImage = null;
MagickImage fLogo = null;
MagickImage sLogo = null;
Dimension imageDim = null;
Dimension logoDim = null;
try {
fImage = new MagickImage(new ImageInfo(filePath));
imageDim = fImage.getDimension();
int width = imageDim.width;
int height = imageDim.height;
if (width > 660) {
height = 660 * height / width;
width = 660;
}
sImage = fImage.scaleImage(width, height);
fLogo = new MagickImage(new ImageInfo(logoPath));
logoDim = fLogo.getDimension();
int lw = width / 8;
int lh = logoDim.height * lw / logoDim.width;
sLogo = fLogo.scaleImage(lw, lh);
sImage.compositeImage(CompositeOperator.AtopCompositeOp, sLogo, width-(lw + lh/10), height-(lh + lh/10));
sImage.setFileName(toImg);
sImage.writeImage(info);
} finally {
if(sImage != null){
sImage.destroyImages();
}
}
}
/**
* 切图
* @param imgPath 源图路径
* @param toPath 修改图路径
* @param w 宽度
* @param h 高度
* @param x
* @param y
* @throws MagickException
*/
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 imgPath 原图片路径
* @param toPath 缩放后的图片路径
* @param w 缩放后图片宽度
* @param h 缩放后图片高度
*/
public static void zoomImg(String imgPath, String toPath, int w, int h) throws Exception{
ImageInfo info = new ImageInfo(imgPath);
MagickImage image = new MagickImage(info);
MagickImage scaleImg = image.scaleImage(w, h);
scaleImg.setFileName(toPath);
scaleImg.writeImage(info);
}
/**
* 水印(文字)
* @param filePath 源文件路径
* @param toImg 修改图路径
* @param text 名字(文字内容自己随意)
* @throws MagickException
*/
public static void initTextToImg(String filePath, String toImg, String text) throws MagickException{
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 wideth = imageDim.width;
int height = imageDim.height;
if (wideth > 660) {
height = 660 * height / wideth;
wideth = 660;
}
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 + 300;
MagickImage scaled = aImage.scaleImage(wideth, height);
if(wideth > tl && height > 5){
DrawInfo aInfo = new DrawInfo(info);
aInfo.setFill(PixelPacket.queryColorDatabase("white"));
aInfo.setUnderColor(new PixelPacket(0,0,0,100));
aInfo.setPointsize(12);
//解决中文乱码问题,自己可以去随意定义个自己喜欢字体,我在这用的微软雅黑
String fontPath = "C:/WINDOWS/Fonts/MSYH.TTF";
// String fontPath = "/usr/maindata/MSYH.TTF";
aInfo.setFont(fontPath);
aInfo.setTextAntialias(true);
aInfo.setOpacity(0);
aInfo.setText(" " + text + "于 " + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "上传于XXXX网,版权归作者所有!");
aInfo.setGeometry("+" + (wideth-tl) + "+" + (height-5));
scaled.annotateImage(aInfo);
}
scaled.setFileName(toImg);
scaled.writeImage(info);
scaled.destroyImages();
}
public static void main(String[] args) throws Exception{
String imgPath = "C:\\images\\bg.jpg";
String toPath = "C:\\images\\bg1.jpg";
ImageHandle.cutImg(imgPath, toPath, 200, 200, 50, 200);
toPath = "C:\\images\\bg2.jpg";
ImageHandle.zoomImg(imgPath, toPath, 400, 300);
toPath = "C:\\images\\bg3.jpg";
String text = "我是孙新";
ImageHandle.initTextToImg(imgPath, toPath, text);
}
}
分享到:
相关推荐
JMagick图片处理 缩略图 水印 切图 压缩图片
jmagick处理图片,图片高质量压缩,图片加水印。
Jmagick32位的和Jmagick64位的Jmagick.dll,Jmagick.jar 图片处理必备工具
文件包含:ImageMagick-6.4.5-3-Q16-windows-dll.exe,jmagick.dll,jmagick.jar,压缩包中的版本统一,安装ImageMagick,复制ImageMagick安装目录的所有.dll文件和Jmagick.dll到,系统的system32下 即可使用,System.set...
开发图片搜索系统时用到的图片缩图解决方案,里面带了例子
强大的图片处理工具,imagemagick的java接口
ImageMagick 是一个功能很强的图片处理程序。可应用在多种操作系统平台上。 安装ImageMagick和JMagick 如果安装了老版本ImageMagick的rpm包,请先删除 安装环境要求: 确认已经安装了zlib、freetype、libpng和jpeg-...
ImageMagick (TM) 是一个免费的创建、编辑、合成图片的软件。它可以读取、转换、写入多种格式的图片。图片切割、颜色替换、各种效果的应用,图片的旋转、组合,文本,直线,多边形,椭圆,曲线,附加到图片伸展旋转...
因为im4java是基于imagemagick的commandline进行调用的java接口,不像 jmagick那样用java jni调用本地imagemagick的api那么复杂。其实安装imagemagick在任何平台都是一样的安装,在linux下面有rpm包,安装那个包,...
Choose from these interfaces: G2F (Ada), MagickCore (C), MagickWand (C), ChMagick (Ch), ImageMagickObject (COM+), Magick++ (C++), JMagick (Java), L-Magick (Lisp), Lua, NMagick (Neko/haXe), Magick.NET...