Java intercepts image part size

Source: Internet
Author: User

Link Address: http://blog.sina.com.cn/s/blog_407a68fc0100nrbl.html

Package util;

Import Java.awt.Rectangle;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.InputStream;
Import Java.util.Iterator;

Import Javax.imageio.ImageIO;
Import Javax.imageio.ImageReadParam;
Import Javax.imageio.ImageReader;
Import Javax.imageio.stream.ImageInputStream;


public class Imgcut {


public void Readusingimagereader (string src, string dest, int w, int h)
Throws Exception {

Get a picture reader
Iterator readers = imageio.getimagereadersbyformatname ("jpg");

ImageReader reader = (imagereader) readers.next ();

Get a picture to read the inflow
InputStream Source = new FileInputStream (SRC);

Imageinputstream IIS = Imageio.createimageinputstream (source);

Reader.setinput (IIS, True);

Picture parameters
Imagereadparam param = Reader.getdefaultreadparam ();
100,200 is the upper left position, 300 is the width of 300, that is, from 100 to 300 wide, that is, transverse 100~400, the same vertical 200~350 area to take the height of 150
Rectangle rect = new Rectangle (100, 200, 300, 150);//
int hh = 0;
if (H > 100)
HH = (h-100)/3;
Rectangle rect = new Rectangle (0, hh, 227, 100);
Param.setsourceregion (rect);

BufferedImage bi = reader.read (0, param);

Imageio.write (BI, "JPG", new File (dest));

}

public static void Main (string[] args) throws Exception {//Main method test with
Imgcut t = new imgcut ();
T.readusingimagereader ("D://2.jpg", "d://3.jpg", 227, 163);
}

}

Java intercepts image part size

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.