ImageMgick & Jmagick
システム環境:archLinux
ImageMagcikはarch pacmanを介して直接インストールするため、基本的に構成されていません.
Jmagickは32ビットオペレーティングシステムの下でlibJMagick.soは直接使用できます.64ビットオペレーティングシステムはソースコードをダウンロードしてコンパイルする必要があります.説明:
(ソースコードのInstallドキュメントを参照してください.autoconfはCのコンパイルソフトウェアにインストールする必要があります)
> autoconf
>./configure
>make all
次にシステムusr/libの下に置きます.
Javaコード:
(コードには他人のblogを引用するものがありますが、どこから引用するか忘れています.
ImageMagcikはarch pacmanを介して直接インストールするため、基本的に構成されていません.
Jmagickは32ビットオペレーティングシステムの下でlibJMagick.soは直接使用できます.64ビットオペレーティングシステムはソースコードをダウンロードしてコンパイルする必要があります.説明:
(ソースコードのInstallドキュメントを参照してください.autoconfはCのコンパイルソフトウェアにインストールする必要があります)
> autoconf
>./configure
>make all
次にシステムusr/libの下に置きます.
Javaコード:
static{
// , jmagick.jar
System.setProperty("jmagick.systemclassloader","no");
}
/**
*
* @param filePath
* @param extName
* @param width
* @param height
* @throws MagickException
*/
public static void createThumbnail(String filePath,String extName, int width,int height) throws MagickException{
ImageInfo info = null;
MagickImage image = null;
MagickImage scaled = null;
try{
//
String file = filePath + "/original-logo." + extName;
info = new ImageInfo(file);
image = new MagickImage(info);
scaled = image.scaleImage(width, height);// .
String slogoName = "/logo." + extName;
scaled.setFileName(filePath + slogoName);
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 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();
}
/**
*
* @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();
}
}
}
(コードには他人のblogを引用するものがありますが、どこから引用するか忘れています.