Java tutorial
/* * @(#)IMagicHelper.java 2011-11-30 * ?us?? 2008-2011, Inc. All rights reserved. * s.server. Use is subject to license terms. */ package com.tools.image; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import magick.CompositeOperator; import magick.CompressionType; import magick.DrawInfo; import magick.ImageInfo; import magick.MagickException; import magick.MagickImage; import magick.PixelPacket; import magick.PreviewType; import org.apache.commons.logging.Log; import com.us.util.Helper; import com.us.util.LogHelper; /** * : API??,<br/> * ? jmagick * * @author <a href="mailto:monlyu.hong@gmail.com">monlyu</a> * @version --0.0.1--, 2011-11-30 * @since JDK6.0 */ public class IMagicHelper extends Helper { static Log log = LogHelper.getLog(); static { System.setProperty("jmagick.systemclassloader", "no"); } /** * * * @param source * ? * @param target * ? * @param width * * @param height * * @throws MagickException * */ public static void createThumbs(File source, File target, int width, int height) throws MagickException { createThumbs(source.getAbsolutePath(), target.getAbsolutePath(), width, height); } public static void createThumbs(String source, String target, int width, int height) throws MagickException { ImageInfo info = new ImageInfo(source); MagickImage image = new MagickImage(info); image.profileImage("*", null);// ?????? MagickImage scaleImg = image.scaleImage(width, height); scaleImg.setFileName(target); scaleImg.writeImage(info); log.info(format("?[%s]->[%s],[W:%s,H:%s]", source, target, width, height)); } /** * ? * * @param source * ? * @param target * * @param width * ?? * @throws IOException * @throws MagickException */ public static void constrainZoom4Width(String source, String target, int width) throws IOException, MagickException { int heigth = constrainSize(source, width, true); createThumbs(source, target, width, heigth); } /** * * * @param source * @param target * @param width * @throws IOException * @throws MagickException */ public static void constrainZoom4Width(File source, File target, int width) throws IOException, MagickException { int heigth = constrainSize(source.getAbsolutePath(), width, true); createThumbs(source, target, width, heigth); } /** * ? * * @param source * ? * @param target * * @param width * ?? * @throws IOException * @throws MagickException */ public static void constrainZoom4Height(String source, String target, int height) throws IOException, MagickException { int width = constrainSize(source, height, false); createThumbs(source, target, width, height); } /** * ?? * * @param source * * @param size * ???/, isWidth ?? * @param isWidth * ?true ??false?? * @return * @throws IOException */ private static int constrainSize(String source, int size, boolean isWidth) throws IOException { int[] rect = getRectangle(source); int length = isWidth ? rect[0] : rect[1]; float rate = (float) size / length; return (int) ((isWidth ? rect[1] : rect[0]) * rate); } // private static int[] getRectangle(String file) throws IOException { BufferedImage tmp = ImageIO.read(new File(file)); return new int[] { tmp.getWidth(), tmp.getHeight() }; } /** * ?(logo) * * @param filePath * ? * @param toImg * * @param logoPath * logo * @param width * * @param height * * @param x * x?? * @param y * Y?? * @throws MagickException */ public static void addLogo2Img(String filePath, String toImg, String logoPath, int width, int height, int x, int y) throws MagickException { ImageInfo info = new ImageInfo(); MagickImage fImage = null; MagickImage sImage = null; MagickImage fLogo = null; try { fImage = new MagickImage(new ImageInfo(filePath)); fImage.profileImage("*", null);// ?????? sImage = fImage.scaleImage(width, height); fLogo = new MagickImage(new ImageInfo(logoPath)); sImage.compositeImage(CompositeOperator.AtopCompositeOp, fLogo, x, y); sImage.setFileName(toImg); sImage.writeImage(info); } finally { if (sImage != null) { sImage.destroyImages(); } } } public static void addLogo2ImgCenter(String filePath, String toImg, String logoPath, int width, int height) throws MagickException { MagickImage fImage = new MagickImage(new ImageInfo(logoPath)); int logow = (int) fImage.getDimension().getWidth(); int logoh = (int) fImage.getDimension().getHeight(); int margin_left = (width - logow) / 2, margin_top = (height - logoh) / 2; addLogo2Img(filePath, toImg, logoPath, width, height, margin_left, margin_top); } public static void addLogo2Img4Width(String filePath, String toImg, String logoPath, int width) throws MagickException, IOException { int height = constrainSize(filePath, width, true); addLogo2ImgCenter(filePath, toImg, logoPath, width, height); } public static void addLogo2Img4Width(File filePath, File toImg, File logoPath, int width) throws MagickException, IOException { addLogo2Img4Width(filePath.getAbsolutePath(), toImg.getAbsolutePath(), logoPath.getAbsolutePath(), width); } /** * ?() * * @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); 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(); } } } public static void main(String[] args) throws MagickException, IOException { // reading image // ImageInfo info = new ImageInfo("C:/data/3410350817826678702.jpg"); // MagickImage image = new MagickImage(info); // MagickImage cropped = null; // // resize image // MagickImage scaleImg = image.scaleImage(451, 300); // // write image to file // scaleImg.setFileName("C:/data/1.jpg"); // scaleImg.writeImage(info); // Rectangle rect = new Rectangle(197, 107, 120, 150); // cropped = scaleImg.cropImage(rect); // cropped.setFileName("C:/data/2.jpg"); // cropped.writeImage(info); // constrainZoom4Width("C:\\Users\\AgraelLee\\1\\1397804734346802808.jpg", // "C:\\Users\\AgraelLee\\1\\3.jpg", 250); // constrainZoom4Height("C:\\Users\\AgraelLee\\1\\1397804734346802808.jpg", // "C:\\Users\\AgraelLee\\1\\2.jpg", 120); // initTextToImg("/Users/monlyu/Pictures//2.jpg", // "/Users/monlyu/5.jpg", "LOVE"); addLogo2Img4Width("C:\\Users\\AgraelLee\\1\\1397804734346802808.jpg", "C:\\Users\\AgraelLee\\1\\logo.jpg", "C:\\Users\\AgraelLee\\1\\l.png", 460); } }