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 ;}