Javaピクチャ処理(Jmagickのアプリケーションを含む)
最近では画像の圧縮処理が行われていますが、以前はJavaで処理していたため、低画素の場合、Java処理の効果は確かに悪かったのですが、ネットで推奨されている無料オープンソースのサードパーティソフトを使ってJavaのjniを利用してdllファイルを呼び出して処理してみましたが、効果はまあまあです.この記録の下で、後で蓄積し続けるのに便利です.
1、純Java クラス処理ピクチャコード
2、Jmagick補助を使う
(下記サイト参照:http://www.i5a6.com/?p=142)
ツールクラス:
1、純Java クラス処理ピクチャコード
/**
* ,
*
* @param img
*
* @param width
*
* @param height
*
*/
public static void changeImge(File img, int width, int height) {
try {
Image image = ImageIO.read(img);
// , , , ,
int srcH = image.getHeight(null);
int srcW = image.getWidth(null);
if (srcH <= height && srcW <= width) {
return;
}
int tmpH = width;
int tmpW = height;
// ,
while (srcH > height || srcW > width) {
if(srcW > width) {
tmpH = srcH * width / srcW;
srcH = tmpH;
srcW=width;
}
if(srcH > height) {
tmpW = srcW * height / srcH;
srcW = tmpW;
srcH=height;
}
}
BufferedImage bufferedImage = new BufferedImage(srcW, srcH,
BufferedImage.TYPE_3BYTE_BGR);
bufferedImage.getGraphics().drawImage(
image.getScaledInstance(srcW, srcH, Image.SCALE_SMOOTH), 0,
0, srcW, srcH, null);
FileOutputStream fos = new FileOutputStream(img);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bufferedImage);
fos.close();
// System.out.println(" ...");
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException(" !", e);
}
}
2、Jmagick補助を使う
(下記サイト参照:http://www.i5a6.com/?p=142)
(1) windows jmagick-win-6.3.9-q16.zip :http://downloads.jmagick.org/6.3.9/
(2)doc api :http://downloads.jmagick.org/jmagick-doc/
(3) imagemagick, :http://www.imagemagick.org/
:imagemagick-6.4.6-4-q16-windows-dll.exe :
(4) imagemagick-6.4.6-4-q16-windows-dll.exe, ( ) dll copy “c:\windows\system32\”
(5)
path “c:\program files\imagemagick-6.4.6-4-q16“ ide
(6) jmagick-win-6.3.9-q16.zip
jmagick.dll “c:\windows\system32\” jdk bin( “d:\jdk6\bin”)
jmagick.jar tomcat lib web-inf lib
(7)web tomcat , catalina.bat
set java_opts=%java_opts% -xms256m -xmx768m -xx:maxpermsize=128m – djava.util.logging.manager=org.apache.juli.classloaderlogmanager – djava.util.logging.config.file=”${catalina.base}\conf\logging.properties”
heap , 。( -xms256m -xmx768m -xx:maxpermsize=128m )
(8) web , class
system.setproperty(“jmagick.systemclassloader”,”no”);
unsatisfiedlinkerror: no jmagick in java.library.path.
ツールクラス:
import java.awt.Dimension;
import java.awt.Rectangle;
import java.text.SimpleDateFormat;
import java.util.Date;
import magick.CompositeOperator;
import magick.CompressionType;
import magick.DrawInfo;
import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;
import magick.PixelPacket;
import magick.PreviewType;
public class ImageUtils {
static{
// , jmagick.jar
System.setProperty("jmagick.systemclassloader","no");
}
/**
* ,
* @param filePath
* @param toPath
* @param width
* @param height
*/
public static void changeSize(String filePath, String toPath,int width,int height) 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 srcH = imageDim.width;
int srcW = imageDim.height;
if (srcH <= height && srcW <= width) {
return;
}
int tmpH = width;
int tmpW = height;
// ,
while (srcH > height || srcW > width) {
if(srcW > width) {
tmpH = srcH * width / srcW;
srcH = tmpH;
srcW=width;
}
if(srcH > height) {
tmpW = srcW * height / srcH;
srcW = tmpW;
srcH=height;
}
}
scaled = image.scaleImage(srcW, srcH);// .
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 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";
// 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 X
* @param y 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();
}
}
}
}