Package test; /** * Processing Watermark Category * * Import java.awt.AlphaComposite; Import Java.awt.Color; Import Java.awt.Graphics2D; Import Java.awt.geom.AffineTransform; Import java.awt.image.AffineTransformOp; Import Java.awt.image.BufferedImage; Import Java.io.ByteArrayOutputStream; Import Java.io.File; Import Java.io.FileInputStream; Import java.io.IOException; Import Java.io.InputStream; Import Javax.imageio.ImageIO; Import Sun.misc.BASE64Decoder; Import Sun.misc.BASE64Encoder; Import Com.sun.image.codec.jpeg.JPEGCodec; Import Com.sun.image.codec.jpeg.JPEGImageEncoder; Public final class Imageutils {private static Base64encoder Base64en = new Base64encoder (); private static Base64decoder BASE64DE = new Base64decoder (); Watermark picture Base64 Code private static string shuiyingimg = "Watermark base64 string, omitted here." "/** * Returns a new Base64 code * @param srcstr Photo Base64 String * @return/public final static string Addwatermark (string, based on Base64 code plus watermark) SRCSTR) throws ioexception,exception{//depending on the size of the Base64 picture being passed, the watermark image size is determined. Process transparent information float alpha = 0.3F; int srcwidth = 0; int srcheight = 0; Byte[] B; try {b = Base64de.decodebuffer (SRCSTR); InputStream is = new Java.io.ByteArrayInputStream (b); BufferedImage src = imageio.read (IS); if (src!= null) {srcwidth = Src.getwidth (null); srcheight = Src.getheight (null); if (srcwidth<=0| | SRCHEIGHT<=0) return null; Change the size of the watermark picture according to the original picture bufferedimage watermark = resize (shuiyingimg,srcwidth,srcheight); /* Add watermark * * bufferedimage img = new Java.awt.image.BufferedImage (Srcwidth, Srcheight,bufferedimage.type_ushort_565_rgb) ; Create artboards graphics2d graph = img.creategraphics (); Print the original image to the drawing board graph.drawimage (src,null,0,0); Set transparency, Alpha Graph.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha)); Draw watermark Picture Graph.drawimage (watermark,null,0,0); /* Convert picture to BYTE * * Bytearrayoutputstream Jpegoutputstream = new Bytearrayoutputstream (); JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (Jpegoutputstream); Encoder.encode (IMG); byte[] Resultbyte = Jpegoutputstream.tobytearray (); Imageio.write (img, "jpg", New File (" d://wave.jpg ")); System.out.println ("Add watermark Complete"); Graph.dispose (); System.out.println (Base64en.encode (resultbyte)); Return Base64en.encode (Resultbyte); }else{return null;} catch (IOException e) {return null;} catch (Exception e) {return null;}} /** * According to the picture size, automatically change the size of the watermark picture. * @param src: * @param w: Original picture Width * @param h: Meta picture height * @return: Return image */public static BufferedImage resize (String src,int w,i NT h) {byte[] b;//load watermark picture in memory try {b = base64de.decodebuffer (SRC); InputStream is = new Java.io.ByteArrayInputStream (b); BufferedImage img = Imageio.read (IS); Get the appropriate scaling ratio, that is, to fully display the contents of the picture in the specified thumbnail size while ensuring the maximum scaling ratio//according to the scale to draw the cached image BufferedImage Mini = new Java.awt.image.BufferedImage (W, H, BUFFEREDIMAGE.TYPE_USHORT_565_RGB); Graphics2D Gmini = Mini.creategraphics (); Gmini.setbackground (Color.White); Let the generated picture transform Gmini.clearrect (0, 0, W, h) by the same proportions; AffineTransform trans = new AffineTransform (); Long and wide simultaneous transformations Trans.scale (double) W/img.getwidth (), (double) h/img.getheight ()); Gmini.setcomposite (ALphacomposite.getinstance (Alphacomposite.src_atop, 0.7f)); Affinetransformop op = new Affinetransformop (trans,affinetransformop.type_bilinear); Gmini.drawimage (IMG, op, 0, 0); Gmini.dispose (); return mini; catch (IOException e) {return null;}} public static void Main (string[] args) {String imgfile = "d://heart.jpg";//The picture to be processed inputstream in = null; byte[] data = Nu ll Read the picture byte array try{in = new FileInputStream (imgfile); data = new byte[in.available ()]; in.read (data); In.close ();} catch (IOException e) {e.printstacktrace ();}//byte array Base64 encoding Base64encoder encoder = new Base64encoder (); try {Imageutils.addwatermark (Encoder.encode (data));} catch (IOException e) {e.printstacktrace ();} catch (Exception e) {E.printstacktrace ();} } }