Image Processing Method Collection

Source: Internet
Author: User

Package com.tools;

Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.Toolkit;
Import Java.awt.image.BufferedImage;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.net.URL;

Import Javax.imageio.ImageIO;
Import Javax.swing.ImageIcon;

/**
* Make simple picture tool
* @author Zhaojun
*/
public class Imagetool {

/**
* Create picture border
* @param width Border Total
* Total height of @param height frame
* @parma borderWidth Border Width
* @param bordercolor border color
* @param Comp Transparency
*/
public static bufferedimage getimageborder (int width, int height, int borderWidth, int bordercolor, float comp) {
BufferedImage img = new BufferedImage (width, height, bufferedimage.type_int_argb);
Graphics2D g2d = Img.creategraphics ();
String rgbstr = integer.tohexstring (bordercolor);
float R = integer.parseint (rgbstr.substring (0, 2), 16);
float g = Integer.parseint (Rgbstr.substring (2, 4), 16);
Float B = Integer.parseint (Rgbstr.substring (4, 6), 16);
G2d.setcolor (New Color (R/0xff, G/0xff, B/0xff, comp));
Top
G2d.fillrect (borderWidth, 0, Width-borderwidth, borderWidth);
Right
G2d.fillrect (Width-borderwidth, BorderWidth, BorderWidth, height-borderwidth);
Below
G2d.fillrect (0, Height-borderwidth, Width-borderwidth, borderWidth);
Left
G2d.fillrect (0, 0, borderWidth, height-borderwidth);
G2d.dispose ();
return img;
}

/**
* Create picture border
* @param OS output stream
* @param formatname picture type (png/jpeg/... )
* @param width Border Total
* Total height of @param height frame
* @parma borderWidth Border Width
* @param bordercolor border color
* @param Comp Transparency
* @throws IOException
*/
public static void Getimageborder (OutputStream os, String formatname, int width, int height, int borderWidth, int Borderco Lor, float comp) throws ioexception{
BufferedImage img = getimageborder (width, height, borderWidth, bordercolor, comp);
Imageio.write (IMG, formatname, OS);
}

/**
* Create picture border
* @param OS output stream
* @param width Border Total
* Total height of @param height frame
* @parma borderWidth Border Width
* @param bordercolor border color
* @param Comp Transparency
* @throws IOException
*/
public static void Getimageborder (OutputStream os, int width, int height, int borderWidth, int bordercolor, float comp) th Rows ioexception{
BufferedImage img = getimageborder (width, height, borderWidth, bordercolor, comp);
Imageio.write (IMG, "PNG", OS);
}

/**
* Image Zoom
* @param srcimg Original
* @param the width of the image after NW scaling
* @param the height of the image after NH zoom
*/
public static BufferedImage Zoomimage (bufferedimage srcimg, int nw, int nh) {
BufferedImage img = null;
img = new BufferedImage (NW, NH, Srcimg.gettype ());
Graphics g = img.getgraphics ();
G.drawimage (srcimg, 0, 0, NW, NH, NULL);
G.dispose ();
return img;
}

/**
* Image Zoom
* @param srcimg Original
* @param the width of the image after NW scaling
* @param the height of the image after NH zoom
*/
public static BufferedImage Zoomimage (Image srcimg, int nw, int nh) {
BufferedImage img = null;
img = new BufferedImage (NW, NH, BUFFEREDIMAGE.TYPE_INT_ARGB);
Graphics g = img.getgraphics ();
G.drawimage (srcimg, 0, 0, NW, NH, NULL);
G.dispose ();
return img;
}

/**
* Image Zoom
* @param srcimg Original
* @param scale Zoom Multiplier
*/
public static BufferedImage Zoomimage (bufferedimage srcimg, float scale) {
New calculation of new picture size
int SW = Srcimg.getwidth ();
int sh = srcimg.getheight ();
int NW = (int) (SW * scale);
int nh = (int) (sh * scale);
Return Zoomimage (Srcimg, NW, NH);
}

/**
* Image Zoom
* @param srcimg Original
* @param the width of the image after NW scaling
*/
public static BufferedImage Zoomimagebynewwidth (bufferedimage srcimg, int nw) {
New calculation of new picture size
int SW = Srcimg.getwidth ();
int sh = srcimg.getheight ();
float scale = NW/(float) SW;
int nh = (int) (sh * scale);
Return Zoomimage (Srcimg, NW, NH);
}

/**
* Image Zoom
* @param srcimg Original
* @param the width of the image after NW scaling
*/
public static BufferedImage Zoomimagebynewwidth (Image srcimg, int nw) {
New calculation of new picture size
Image _srcimg = new ImageIcon (srcimg). GetImage ();
int SW = _srcimg.getwidth (NULL);
int sh = _srcimg.getheight (null);
float scale = NW/(float) SW;
int nh = (int) (sh * scale);
Return Zoomimage (_srcimg, NW, NH);
}

/**
* Image Zoom
* @param srcimg Original
* @param the width of the image after NW scaling
* @throws IOException
*/
public static void Zoomimagebynewwidth (OutputStream os, URL srcimg, int nw) throws ioexception{
Using Imageio.read () to read a picture will lose some of the information, the resulting picture feels like a layer of things (some say that the ICC information is lost when reading),
So use Toolkit.getdefaulttoolkit (). GetImage ()
BufferedImage img = imagetool.zoomimagebynewwidth (Toolkit.getdefaulttoolkit (). GetImage (srcimg), NW);
Imageio.write (IMG, "PNG", OS);
}


public static void Main (string[] args) {

try {
Imagetool.getimageborder (New FileOutputStream ("E:/v_border_n.png"),
174, 5, 0X3FA1DD, 0.8f);

String url = "Http://192.168.136.80:8080/remote-support/convert_files/video/201504/63f28cf5d8704c9188d4c7efe75134b3.jpg";
Imagetool.zoomimagebynewwidth (New FileOutputStream ("E:/v_yyy.jpg"), new URL (URL), 170);

BufferedImage img = imagetool.zoomimagebynewwidth (Toolkit.getdefaulttoolkit (). GetImage (New URL (
"Http://192.168.136.80:8080/remote-support/convert_files/video/201504/63f28cf5d8704c9188d4c7efe75134b3.jpg")), 170);
Imageio.write (IMG, "PNG", New FileOutputStream ("E:/v_xxx.png"));

} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}

Image Processing Method Collection

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.