Java thumbnail, image watermark, Text watermark map

Source: Internet
Author: User
 
Package com.java.utils;
Import Java.awt.AlphaComposite;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import java.awt.RenderingHints;
Import Java.awt.geom.AffineTransform;
Import Java.awt.image.BufferedImage;
Import Java.awt.image.ColorModel;
Import Java.awt.image.WritableRaster;
Import Java.io.File;
Import Java.io.FileOutputStream;

Import java.io.IOException;
Import Javax.imageio.ImageIO;
Import Com.sun.image.codec.jpeg.JPEGCodec;

Import Com.sun.image.codec.jpeg.JPEGImageEncoder; public class Imagehelper {/** * Generate thumbnails <br/> Save: Imageio.write (BufferedImage, imgtype[jpg/png/...),
         File);  * @param source * Original picture * @param width * thumbnail width * @param Height * thumbnail Height * @param b * Whether equal scaling * * */public static Bu Fferedimage Thumb (bufferedimage source, int width, int hEight, Boolean B) {//Targetw,targeth respectively represents the target long and wide int type = Source.gettype ();
                BufferedImage target = null;
                Double SX = (double) width/source.getwidth ();

                Double sy = (double) height/source.getheight ();
                                if (b) {if (sx > sy) {sx = SY;
                        width = (int) (SX * SOURCE.GETWIDTH ());
                                else {sy = SX;
                        Height = (int) (SY * source.getheight ());
                        } if (type = = Bufferedimage.type_custom) {//Handmade
                        ColorModel cm = Source.getcolormodel ();
                        WritableRaster raster = cm.createcompatiblewritableraster (width, height); Boolean alphapremultiplied = cm. isalphapremultiplied ();
                target = new BufferedImage (cm, raster, alphapremultiplied, null);
                else target = new BufferedImage (width, height, type);
                Graphics2D g = target.creategraphics (); Smoother than Exlax:g.setrenderinghint (renderinghints.key_rendering, R
                enderinghints.value_render_quality);
                G.drawrenderedimage (source, affinetransform.getscaleinstance (SX, SY));
                G.dispose ();
        return target; /** * Image Watermark * * @param imgpath * Pending picture * @param Markpat            H * Watermark Image * @param x * watermark is located in the upper-left corner of the image x coordinate value * @param y * Watermark is at the y-coordinate of the upper-left corner of the picture * @param alpha * watermark Transparency 0.1f ~ 1.0f */Public stat IC void Watermark (String imgpath, String markpath, int x, int y, float Alpha) {try {//Load pending graph

                        Piece file Image img = imageio.read (new file (Imgpath)); BufferedImage image = New BufferedImage (Img.getwidth (NULL), IMG. getheight (NULL),
                        BUFFEREDIMAGE.TYPE_INT_RGB);
                        Graphics2D g = image.creategraphics ();

                        G.drawimage (IMG, 0, 0, NULL);
                        Load watermark picture file image Src_biao = imageio.read (new file (Markpath));
                        G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha));
                        G.drawimage (Src_biao, x, y, null);

                        G.dispose ();
                        Save the processed file FileOutputStream out = new FileOutputStream ("d://jinlei.jpg"); JPEGImageEncoder Encoder = Jpegcodec.createjpegencoder (out);
                        Encoder.encode (image);
                Out.close ();
                catch (Exception e) {e.printstacktrace (); }/** * Text watermark * * @param imgpath * Pending picture * @par            AM Text * watermark Text * @param font * Watermark Font information * @param color * Watermark Font Color * @param x * watermark is located in the upper-left corner of the picture x coordinate * @param y * watermark bit The y-coordinate value in the upper-left corner of the picture * @param alpha * watermark Transparency 0.1f ~ 1.0f */public static void text
                Mark (String Imgpath, string text, font font, color color, int x, int y, float alpha) {

                        try {font Dfont = (font = NULL)? New Font ("XXFarEastFont-Arial"): Font; Image img = imageio.read (New File (Imgpath)); BufferedImage image = New BufferedImage (Img.getwidth (NULL), IMG. getheight (NULL),
                        BUFFEREDIMAGE.TYPE_INT_RGB);

                        Graphics2D g = image.creategraphics ();
                        G.drawimage (IMG, 0, 0, NULL);
                        G.setcolor (color);
                        G.setfont (Dfont);
                        G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha));
                        g.DrawString (text, x, y);
                        G.dispose ();
                        FileOutputStream out = new FileOutputStream ("d://jinlei.jpg");
                        JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
                        Encoder.encode (image);
                Out.close ();
                catch (Exception e) {System.out.println (e);
        
        }
        }public static void Main (string[] args) throws IOException {//Font Titlefont = new Font ("Arial", Font.Bold, 30);
        	
New Imagehelper (). Textmark ("D://jl.jpg", "good description, yes, one more, what do you want to text", Titlefont, color.red, MB, 1.0f);
		New Imagehelper (). Watermark ("D://jl.jpg", "d://sy.jpg", MB, 0.5f); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.