Watermark scaling and input stream conversion

Source: Internet
Author: User

Watermark scaling and input stream conversion

Watermarks and zooming images:

Public final static BufferedImage [] pressImage (InputStream srcImg, String waterImg, float alpha) throws IOException {// File file = new File (targetImg); Image image = ImageIO. read (srcImg); int width = image. getWidth (null); int height = image. getHeight (null); BufferedImage bufferedImage = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); Graphics2D g = bufferedImage. createGraphics (); g. drawImage (image, 0, 0, width, height, null); Image waterImage = ImageIO. read (new File (waterImg); // watermark File int width_1 = waterImage. getWidth (null); int height_1 = waterImage. getHeight (null); g. setComposite (AlphaComposite. getInstance (AlphaComposite. SRC_ATOP, alpha); int div = (int) (0.1 * width_1); for (int y = 0, row = 0; y
 
  
The Thumbnails tool is used for image scaling (it can also be used for watermarking, but I have not studied it yet)

Search for the jar package or maven by yourself

Note that Thumbnails scales proportionally by default and Its Scaling Rules are as follows:

Assume that the image is placed in 200*300

* If the image horizontal ratio is smaller than 200, the aspect ratio is smaller than 300, and the image remains unchanged.
* If the image horizontal ratio is smaller than 200, the aspect ratio is greater than 300, and the height is reduced to 300, the image proportion remains unchanged.
* If the image size is larger than 200, the image height is smaller than 300, and the image size is reduced to 200, the image size remains unchanged.
* If the image size is larger than 200, the image height is greater than 300, and the image size decreases proportionally. The horizontal value is 200 or the height is 300.

BufferInage conversion inputSteam

Private InputStream getInputstreamFromBufferedImage (BufferedImage img) throws IOException {ByteArrayOutputStream bs = new ByteArrayOutputStream (); ImageOutputStream imOut = ImageIO. createImageOutputStream (bs); ImageIO. write (img, "jpg", imOut); // scaledImage1 is BufferedImage, jpg is the image type InputStream is = new ByteArrayInputStream (bs. toByteArray (); return is ;}



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.