Java image-Processing truncated graphics (modified version)

Source: Internet
Author: User
Tags truncated


You can use this test when you have a network

Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import java.awt.RenderingHints;
Import Java.awt.geom.Ellipse2D;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import java.net.ProtocolException;

Import Java.net.URL;

Import Javax.imageio.ImageIO;
	
	The public class Test {static String URL = "http://avatar.csdn.net/3/1/7/1_qq_27292113.jpg?1488183229974"; public static void Main (string[] args) {try {//http://avatar.csdn.net/3/1/7/1_qq_27292113.jpg?1488183229974// is avatar address//Get picture stream bufferedimage url = geturlbybufferedimage ("http://avatar.csdn.net/3/1/7/1_qq_27292113.jpg?14881

83229974 ");
Image src = imageio.read (new File ("C:/users/administrator/desktop/imag.png"));
			BufferedImage URL = (bufferedimage) src;
			Process the picture to compress it into a square small figure bufferedimage convertimage = scalebypercentage (URL, 100, 100); Cropped into a circle (the incoming image must be square toIf the circle is rectangular, the proportions will become elliptical) Convertimage = convertcircular (URL);
			Generated picture position String ImagePath = "C:/users/administrator/desktop/imag.png";
			Imageio.write (Convertimage, imagepath.substring (Imagepath.lastindexof (".") + 1), new File (ImagePath));
		System.out.println ("OK");
		catch (Exception e) {e.printstacktrace (); }/** * Reduces image, this method returns images of the source image scaled by given width, height limit * @param inputimage * @param maxwidth *: Compressed width * @param maxheight *: Compressed Height * @throws java.io.IOException * return/public static buffered Image Scalebypercentage (bufferedimage inputimage, int newwidth, int newheight) throws Exception {//Get original image transparency type int T
		ype = Inputimage.getcolormodel (). Gettransparency ();
		int width = inputimage.getwidth ();
		int height = inputimage.getheight (); Open anti-aliasing renderinghints renderinghints = new Renderinghints (renderinghints.key_antialiasing, Renderinghints.value_
		ANTIALIAS_ON); Use high quality compression renderinghints.put (RenderinghinTs.
		Key_rendering, renderinghints.value_render_quality);
		BufferedImage img = new BufferedImage (newwidth, newheight, type);
		Graphics2D graphics2d = Img.creategraphics ();
		Graphics2d.setrenderinghints (renderinghints);
		Graphics2d.drawimage (inputimage, 0, 0, newwidth, newheight, 0, 0, width, height, null);
		Graphics2d.dispose ();
	return img; 
		/** * Get picture via network * * @param URL * @return/public static bufferedimage geturlbybufferedimage (String URL) {
			try {URL urlobj = new URL (URL);
			HttpURLConnection conn = (httpurlconnection) urlobj.openconnection ();
			Connection Timeout Conn.setdoinput (true);
			Conn.setdooutput (TRUE);
			Conn.setconnecttimeout (25000);
			Read timeout-server response is slow, increase time conn.setreadtimeout (25000);
			Conn.setrequestmethod ("get");
			Conn.addrequestproperty ("Accept-language", "ZH-CN");
			Conn.addrequestproperty ("Content-type", "image/jpeg"); Conn.addrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 5.1; trident/4.0. NET CLR 2.0.50727) ");
			Conn.connect ();
			BufferedImage bufimg = Imageio.read (Conn.getinputstream ());
			Conn.disconnect ();
		return bufimg;
		catch (Malformedurlexception e) {e.printstacktrace ();
		catch (Protocolexception e) {e.printstacktrace ();
		catch (IOException e) {e.printstacktrace ();
	return null; /** * Incoming images must be square to be rounded if the proportions of the rectangles will become oval * * @param URL * User Avatar Address * @return * @throws ioexcept Ion */public static bufferedimage Convertcircular (BufferedImage bi1) throws IOException {//BufferedImage BI1 = I
		
		Mageio.read (new File (URL));

		This is the black Bottom//bufferedimage Bi2 = new BufferedImage (Bi1.getwidth (), Bi1.getheight (), BUFFEREDIMAGE.TYPE_INT_RGB);
		Transparent bottom of the picture bufferedimage Bi2 = new BufferedImage (Bi1.getwidth (), Bi1.getheight (), BUFFEREDIMAGE.TYPE_4BYTE_ABGR);
		ellipse2d.double shape = new ellipse2d.double (0, 0, bi1.getwidth (), Bi1.getheight ());
		graphics2d g2 = Bi2.creategraphics ();
		G2.setclip (Shape); Using SETREnderinghint set anti-aliasing g2.drawimage (bi1, 0, 0, NULL);
		Set color G2.setbackground (Color.green);
		G2.dispose ();
	return BI2;
 }
}

There is no Internet use this test (local must have c:/users/administrator/desktop/imag.png this picture or it will be an error)
	public static void Main (string[) args {
		try {
			//Picture's local address
	        image src = imageio.read (new File ("c:/users/ Administrator/desktop/imag.png "));
	        BufferedImage URL = (bufferedimage) src;
	        Process the picture to compress it into a square small figure
	        bufferedimage  convertimage= scalebypercentage (URL, 100,100);
	        Cropped into a circle (the incoming image must be square to be rounded if the proportions of the rectangle will become elliptical)
	        convertimage = convertcircular (URL);
	        The generated picture position
	    	String imagepath= "C:/users/administrator/desktop/imag.png";
	        Imageio.write (Convertimage, imagepath.substring (Imagepath.lastindexof (".") + 1), new File (ImagePath)); 
	        System.out.println ("OK");
		} catch (Exception e) {
			e.printstacktrace ();
		}
	}

Comparison of execution results



Here's a reminder that this clipping actually just preserves the tones in the circle and the rest of it turns into a transparent hue, and it's not cropped out (plainly or in the original shape).

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.