功能描述
图片裁剪 图片尺寸放大 图片尺寸缩小 图片尺寸重置 图片灰化 添加文字水印 添加图片水印
package com. qiangesoft. watermark. utils ;
import org. springframework. util. Assert ;
import org. springframework. web. multipart. MultipartFile ;
import javax. imageio. ImageIO ;
import javax. imageio. ImageReadParam ;
import javax. imageio. ImageReader ;
import javax. imageio. stream. ImageInputStream ;
import java. awt. * ;
import java. awt. color. ColorSpace ;
import java. awt. image. BufferedImage ;
import java. awt. image. ColorConvertOp ;
import java. io. * ;
import java. util. Iterator ;
public class ImageOperateUtil {
public static void cropImage ( String sourcePath, String targetPath, int width, int height) throws IOException {
cropImage ( sourcePath, targetPath, 0 , 0 , width, height) ;
}
public static void cropImage ( String sourcePath, String targetPath, int x, int y, int width, int height) throws IOException {
String sourceType = sourcePath. substring ( sourcePath. lastIndexOf ( "." ) + 1 ) ;
String targetType = targetPath. substring ( targetPath. lastIndexOf ( "." ) + 1 ) ;
try ( ImageInputStream iis = ImageIO . createImageInputStream ( new File ( sourcePath) ) ;
FileOutputStream fos = new FileOutputStream ( targetPath) ) {
cropImage ( iis, fos, sourceType, targetType, x, y, width, height) ;
}
}
public static void cropImage ( MultipartFile file, OutputStream outputStream, int width, int height) throws IOException {
cropImage ( file, outputStream, 0 , 0 , width, height) ;
}
public static void cropImage ( MultipartFile file, OutputStream outputStream, int x, int y, int width, int height) throws IOException {
String originalFilename = file. getOriginalFilename ( ) ;
Assert . notNull ( originalFilename, "文件非法" ) ;
String sourceType = originalFilename. substring ( originalFilename. lastIndexOf ( "." ) + 1 ) ;
try ( ImageInputStream iis = ImageIO . createImageInputStream ( file. getInputStream ( ) ) ) {
cropImage ( iis, outputStream, sourceType, sourceType, x, y, width, height) ;
}
}
private static void cropImage ( ImageInputStream inputStream, OutputStream outputStream, String sourceType, String targetType, int x, int y, int width, int height) throws IOException {
Iterator < ImageReader > readerIterator = ImageIO . getImageReadersByFormatName ( sourceType) ;
Assert . isTrue ( readerIterator. hasNext ( ) , "图片文件格式错误" ) ;
ImageReader imageReader = readerIterator. next ( ) ;
imageReader. setInput ( inputStream) ;
ImageReadParam param = imageReader. getDefaultReadParam ( ) ;
param. setSourceRegion ( new Rectangle ( x, y, width, height) ) ;
BufferedImage bufferedImage = imageReader. read ( 0 , param) ;
ImageIO . write ( bufferedImage, targetType, outputStream) ;
}
public static void reduceImageByRatio ( String sourcePath, String targetPath, int ratio) throws IOException {
String fileType = targetPath. substring ( targetPath. lastIndexOf ( "." ) + 1 ) ;
try ( FileInputStream inputStream = new FileInputStream ( sourcePath) ;
FileOutputStream outputStream = new FileOutputStream ( targetPath) ) {
reduceImageByRatio ( inputStream, outputStream, ratio, fileType) ;
}
}
public static void reduceImageByRatio ( MultipartFile file, OutputStream outputStream, int ratio) throws IOException {
String originalFilename = file. getOriginalFilename ( ) ;
Assert . notNull ( originalFilename, "文件非法" ) ;
String fileType = originalFilename. substring ( originalFilename. lastIndexOf ( "." ) + 1 ) ;
try ( InputStream inputStream = file. getInputStream ( ) ) {
reduceImageByRatio ( inputStream, outputStream, ratio, fileType) ;
}
}
private static void reduceImageByRatio ( InputStream inputStream, OutputStream outputStream, int ratio, String sourceType) throws IOException {
BufferedImage sourceImage = ImageIO . read ( inputStream) ;
int width = sourceImage. getWidth ( ) / ratio;
int height = sourceImage. getHeight ( ) / ratio;
resizeImage ( sourceImage, outputStream, width, height, sourceType) ;
}
public static void enlargeImageByRatio ( String sourcePath, String targetPath, int ratio) throws IOException {
String fileType = targetPath. substring ( targetPath. lastIndexOf ( "." ) + 1 ) ;
try ( FileInputStream inputStream = new FileInputStream ( sourcePath) ;
FileOutputStream outputStream = new FileOutputStream ( targetPath) ) {
enlargeImageByRatio ( inputStream, outputStream, ratio, fileType) ;
}
}
public static void enlargeImageByRatio ( MultipartFile file, OutputStream outputStream, int ratio) throws IOException {
String originalFilename = file. getOriginalFilename ( ) ;
Assert . notNull ( originalFilename, "文件非法" ) ;
String fileType = originalFilename. substring ( originalFilename. lastIndexOf ( "." ) + 1 ) ;
try ( InputStream inputStream = file. getInputStream ( ) ) {
enlargeImageByRatio ( inputStream, outputStream, ratio, fileType) ;
}
}
private static void enlargeImageByRatio ( InputStream inputStream, OutputStream outputStream, int ratio, String fileType) throws IOException {
BufferedImage sourceImage = ImageIO . read ( inputStream) ;
int width = sourceImage. getWidth ( ) * ratio;
int height = sourceImage. getHeight ( ) * ratio;
resizeImage ( sourceImage, outputStream, width, height, fileType) ;
}
public static void resizeImage ( String sourcePath, String targetPath, int width, int height) throws IOException {
String fileType = targetPath. substring ( targetPath. lastIndexOf ( "." ) + 1 ) ;
try ( FileInputStream inputStream = new FileInputStream ( sourcePath) ;
FileOutputStream outputStream = new FileOutputStream ( targetPath) ) {
BufferedImage sourceImage = ImageIO . read ( inputStream) ;
resizeImage ( sourceImage, outputStream, width, height, fileType) ;
}
}
public static void resizeImage ( MultipartFile file, OutputStream outputStream, int width, int height) throws IOException {
String originalFilename = file. getOriginalFilename ( ) ;
Assert . notNull ( originalFilename, "文件非法" ) ;
String fileType = originalFilename. substring ( originalFilename. lastIndexOf ( "." ) + 1 ) ;
try ( InputStream inputStream = file. getInputStream ( ) ) {
BufferedImage sourceImage = ImageIO . read ( inputStream) ;
resizeImage ( sourceImage, outputStream, width, height, fileType) ;
}
}
private static void resizeImage ( BufferedImage sourceImage, OutputStream outputStream, int width, int height, String fileType) throws IOException {
BufferedImage targetImage = new BufferedImage ( width, height, BufferedImage . TYPE_INT_RGB ) ;
targetImage. getGraphics ( ) . drawImage ( sourceImage, 0 , 0 , null ) ;
ImageIO . write ( targetImage, fileType, outputStream) ;
}
public static void grayImage ( String sourcePath, String targetPath) throws IOException {
String fileType = targetPath. substring ( targetPath. lastIndexOf ( "." ) + 1 ) ;
try ( FileInputStream inputStream = new FileInputStream ( sourcePath) ;
FileOutputStream outputStream = new FileOutputStream ( targetPath) ) {
grayImage ( inputStream, outputStream, fileType) ;
}
}
public static void grayImage ( MultipartFile file, OutputStream outputStream) throws IOException {
String originalFilename = file. getOriginalFilename ( ) ;
Assert . notNull ( originalFilename, "文件非法" ) ;
String fileType = originalFilename. substring ( originalFilename. lastIndexOf ( "." ) + 1 ) ;
try ( InputStream inputStream = file. getInputStream ( ) ) {
grayImage ( inputStream, outputStream, fileType) ;
}
}
private static void grayImage ( InputStream inputStream, OutputStream outputStream, String fileType) throws IOException {
BufferedImage bufferedImage = ImageIO . read ( inputStream) ;
ColorSpace cs = ColorSpace . getInstance ( ColorSpace . CS_GRAY ) ;
ColorConvertOp op = new ColorConvertOp ( cs, null ) ;
BufferedImage newBufferedImage = op. filter ( bufferedImage, null ) ;
ImageIO . write ( newBufferedImage, fileType, outputStream) ;
}
public static void watermarkText ( String sourcePath, String targetPath, Font font, Color color, int x, int y, String watermarkText) throws IOException {
String fileType = targetPath. substring ( targetPath. lastIndexOf ( "." ) + 1 ) ;
try ( FileInputStream inputStream = new FileInputStream ( sourcePath) ;
FileOutputStream outputStream = new FileOutputStream ( targetPath) ) {
watermarkText ( inputStream, outputStream, font, color, x, y, watermarkText, fileType) ;
}
}
public static void watermarkText ( MultipartFile file, OutputStream outputStream, Font font, Color color, int x, int y, String watermarkText) throws IOException {
String originalFilename = file. getOriginalFilename ( ) ;
Assert . notNull ( originalFilename, "文件非法" ) ;
String fileType = originalFilename. substring ( originalFilename. lastIndexOf ( "." ) + 1 ) ;
try ( InputStream inputStream = file. getInputStream ( ) ) {
watermarkText ( inputStream, outputStream, font, color, x, y, watermarkText, fileType) ;
}
}
private static void watermarkText ( InputStream inputStream, OutputStream outputStream, Font font, Color color, int x, int y, String watermarkText, String fileType) throws IOException {
BufferedImage image = ImageIO . read ( inputStream) ;
Graphics2D g2d = image. createGraphics ( ) ;
g2d. setFont ( font) ;
g2d. setColor ( color) ;
g2d. drawString ( watermarkText, x, y) ;
g2d. dispose ( ) ;
ImageIO . write ( image, fileType, outputStream) ;
}
public static void watermarkImage ( String sourcePath, String appendSourcePath, String targetPath, int x, int y, int width, int height) throws IOException {
String fileType = targetPath. substring ( targetPath. lastIndexOf ( "." ) + 1 ) ;
try ( FileInputStream inputStream = new FileInputStream ( sourcePath) ;
FileInputStream appendInputStream = new FileInputStream ( appendSourcePath) ;
FileOutputStream outputStream = new FileOutputStream ( targetPath) ) {
watermarkImage ( inputStream, appendInputStream, outputStream, x, y, width, height, fileType) ;
}
}
public static void watermarkImage ( MultipartFile file, MultipartFile appendFile, OutputStream outputStream, int x, int y, int width, int height) throws IOException {
String originalFilename = file. getOriginalFilename ( ) ;
Assert . notNull ( originalFilename, "文件非法" ) ;
String fileType = originalFilename. substring ( originalFilename. lastIndexOf ( "." ) + 1 ) ;
try ( InputStream inputStream = file. getInputStream ( ) ;
InputStream appendInputStream = appendFile. getInputStream ( ) ) {
watermarkImage ( inputStream, appendInputStream, outputStream, x, y, width, height, fileType) ;
}
}
private static void watermarkImage ( InputStream inputStream, InputStream appendInputStream, OutputStream outputStream, int x, int y, int width, int height, String fileType) throws IOException {
BufferedImage image = ImageIO . read ( inputStream) ;
Graphics2D g2d = image. createGraphics ( ) ;
BufferedImage appendImage = ImageIO . read ( appendInputStream) ;
g2d. drawImage ( appendImage, x, y, width, height, null , null ) ;
g2d. dispose ( ) ;
ImageIO . write ( image, fileType, outputStream) ;
}
public static void main ( String [ ] args) throws Exception {
ImageOperateUtil . cropImage ( "C:/Users/16/Desktop/image/mianju.jpg" , "C:/Users/16/Desktop/image/mianju1.jpg" , 2000 , 1000 ) ;
ImageOperateUtil . cropImage ( "C:/Users/16/Desktop/image/mianju.jpg" , "C:/Users/16/Desktop/image/mianju2.jpg" , 500 , 500 , 2000 , 1000 ) ;
ImageOperateUtil . reduceImageByRatio ( "C:/Users/16/Desktop/image/mianju.jpg" , "C:/Users/16/Desktop/image/mianju3.jpg" , 2 ) ;
ImageOperateUtil . enlargeImageByRatio ( "C:/Users/16/Desktop/image/mianju.jpg" , "C:/Users/16/Desktop/image/mianju4.jpg" , 2 ) ;
ImageOperateUtil . resizeImage ( "C:/Users/16/Desktop/image/mianju.jpg" , "C:/Users/16/Desktop/image/mianju5.jpg" , 3000 , 2000 ) ;
ImageOperateUtil . grayImage ( "C:/Users/16/Desktop/image/mianju.jpg" , "C:/Users/16/Desktop/image/mianju6.jpg" ) ;
ImageOperateUtil . watermarkText ( "C:/Users/16/Desktop/image/mianju.jpg" , "C:/Users/16/Desktop/image/mianju7.jpg" ,
new Font ( "宋体" , Font . BOLD , 150 ) , new Color ( 255 , 255 , 255 , 255 ) , 300 , 500 , "哈哈哈哈" ) ;
ImageOperateUtil . watermarkImage ( "C:/Users/16/Desktop/image/mianju.jpg" , "C:/Users/16/Desktop/image/demo.jpeg" ,
"C:/Users/16/Desktop/image/mianju8.jpg" , 0 , 0 , 300 , 200 ) ;
}
}