Add watermarks, resize images, cut images, and scale watermarks
# Adding a watermark to an image
# Image watermark text
# Resize the source Image
Import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. image; import java. awt. image. bufferedImage; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import javax. imageio. imageIO; import com.sun.image.codec.jpeg. export codec; import com.sun.image.codec.jpeg. required imageencoder; // image operation, watermark public class ImageOperate {public static void main (String [] args) throws IOException {ImageOperate. waterMark ("/Users/kevin/Downloads/test1.jpg", "/Users/kevin/Downloads/test.png"); ImageOperate. waterMarkTxt ("/Users/kevin/Downloads/test1.jpg", "http://blog.csdn.net/kevin_luan"); ImageOperate. zoomImage ("/Users/kevin/Downloads/test1.jpg", 300,200);}/*** Add a watermark to the image, but do not change the size ** @ param strOriginalFileName * Original file * @ param strWaterMarkFileName * waterMark */public static void waterMark (String originalPath, String waterMarkPath) {try {File fileOriginal = new File (originalPath); Image imageOriginal = ImageIO. read (fileOriginal); int widthOriginal = imageOriginal. getWidth (null); int heightOriginal = imageOriginal. getHeight (null); BufferedImage bufImage = new BufferedImage (widthOriginal, heightOriginal, BufferedImage. TYPE_INT_RGB); Graphics g = bufImage. createGraphics (); g. drawImage (imageOriginal, 0, 0, widthOriginal, heightOriginal, null); // watermark File fileWaterMark = new File (waterMarkPath); Image imageWaterMark = ImageIO. read (fileWaterMark); int widthWaterMark = imageWaterMark. getWidth (null); int heightWaterMark = imageWaterMark. getHeight (null); // watermark file in the bottom right corner of the source file g. drawImage (imageWaterMark, widthOriginal-widthWaterMark, heightOriginal-heightWaterMark, widthWaterMark, heightWaterMark, null); g. dispose (); FileOutputStream fos = new FileOutputStream (originalPath); required imageencoder encoder = required codec. createJPEGEncoder (fos); encoder. encode (bufImage); fos. flush (); fos. close (); fos = null;} catch (Exception e) {e. printStackTrace () ;}}/*** add watermark text to the image ** @ param strOriginalFileName * Original file * @ param strWaterMarkFileName * watermark */public static void waterMarkTxt (String originalPath, string data) {try {File fileOriginal = new File (originalPath); Image imageOriginal = ImageIO. read (fileOriginal); int widthOriginal = imageOriginal. getWidth (null); int heightOriginal = imageOriginal. getHeight (null); BufferedImage bufImage = new BufferedImage (widthOriginal, heightOriginal, BufferedImage. TYPE_INT_RGB); Graphics g = bufImage. createGraphics (); g. drawImage (imageOriginal, 0, 0, widthOriginal, heightOriginal, null); g. setColor (Color. red); g. setFont (new Font ("", Font. BOLD, 20); g. drawString (data, (int) (widthOriginal/2), heightOriginal-20); g. dispose (); FileOutputStream fos = new FileOutputStream (originalPath); required imageencoder encoder = required codec. createJPEGEncoder (fos); encoder. encode (bufImage); fos. flush (); fos. close (); fos = null;} catch (Exception e) {e. printStackTrace () ;}/ *** zoom image *** @ throws IOException */public static String zoomImage (String srcImgFile, int width, int height) throws IOException {java. io. file file = new java. io. file (srcImgFile); int splitIndex = srcImgFile. indexOf (". "); String path = srcImgFile. substring (0, splitIndex) + width + "_" + height + srcImgFile. substring (splitIndex); Image src = javax. imageio. imageIO. read (file); java. awt. image. bufferedImage bufferedImage = new java. awt. image. bufferedImage (width, height, java. awt. image. bufferedImage. TYPE_INT_RGB); bufferedImage. getGraphics (). drawImage (src, 0, 0, width, height, null); FileOutputStream newimage = new FileOutputStream (path); required imageencoder encoder = required codec. createJPEGEncoder (newimage); encoder. encode (bufferedImage); // near JPEG encoded newimage. close (); return path ;}}
# Build a simple image cutting service based on Spring MVN
Import java. awt. graphics; import java. awt. image; import java. awt. image. bufferedImage; import java. io. IOException; import java.net. URL; import javax. imageio. imageIO; import javax. servlet. http. httpServletResponse; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. pathVariable; import org. springframework. web. bind. annotation. requestMapping;/*** cut Image Service ** @ author Kevin luan **/@ Controllerpublic class ImageScaleController {/*** cut image access URL: * http: // localhost: 8080/h5/scale_110x111? ImgUrl = http://www.mdtit *. com/frontend/images/slider1.jpg ** @ param response * @ param width * @ param height * @ param imgUrl * Cut image URL * @ throws IOException */@ RequestMapping (value = "/scale _ {width} x {height }") public void scale (HttpServletResponse response, @ PathVariable int width, @ PathVariable int height, String imgUrl) throws IOException {BufferedImage image = ImageIO. read (new URL (imgUrl); image = scale (image, width, height); response. setDateHeader ("Expires", System. currentTimeMillis () + 2592000000L); response. setContentType ("image/png"); ImageIO. write (image, "png", response. getOutputStream (); response. getOutputStream (). flush (); response. getOutputStream (). close ();}/*** crop image ** @ param image * @ param x * @ param y * @ return */private BufferedImage scale (BufferedImage image, int x, int y) {int height = y; int width = x; if (height = 0) {height = (int) (width/getXYRatioOf (image ));} else if (height <0) {height =-height * width; width = (int) (height/getXYRatioOf (image);} if (needResize (image, width, height) {if (needCut (image, width, height) {image = cutImage (image, width, height);} if (needResize (image, width, height )) {image = resizeImage (image, width, height) ;}} return image;} BufferedImage resizeImage (BufferedImage image, int width, int height) {int y = (int) (1.0f * image. getHeight () * width)/image. getWidth (); Image result = image. getScaledInstance (width, y, BufferedImage. SCALE_DEFAULT); if (result instanceof BufferedImage) {return (BufferedImage) result;} else {BufferedImage bi = new BufferedImage (width, y, BufferedImage. TYPE_INT_RGB); Graphics graphics = bi. getGraphics (); graphics. drawImage (result, 0, 0, null); graphics. dispose (); return bi ;}} boolean needResize (BufferedImage image, int maxX, int maxY) {return image. getWidth ()> maxX | image. getHeight ()> maxY * 3;} BufferedImage cutImage (BufferedImage image, int wantX, int wantY) {int width = image. getWidth (); int height = image. getHeight (); int startX = 0; int startY = 0; float wantRatio = getRatio (wantX, wantY); float imageRatio = getXYRatioOf (image); if (isTooHigh (imageRatio, wantRatio) {height = (int) (width/wantRatio); startY = (image. getHeight ()-height)/2;} else {width = (int) (height * wantRatio); if (width <wantX) {width = wantX;} startX = (image. getWidth ()-width)/2;} return image. getSubimage (startX, startY, width, height);} boolean isTooHigh (float realRatio, float wantRatio) {return wantRatio> realRatio;} boolean needCut (BufferedImage image, int wantX, int wantY) {float ratio = getXYRatioOf (image); float wantRatio = getRatio (wantX, wantY); return wantRatio> ratio + 0.01f | wantRatio <ratio-0.01f ;} private float getXYRatioOf (BufferedImage image) {return getRatio (image. getWidth (), image. getHeight ();} private float getRatio (int x, int y) {return (float) x)/y ;}}