import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
/**
* @Description 圖片處理
*/
public class GraphicsUtil {
/**
* @Description 兩張圖片合併成一張圖片
* @Param [blankUrl:底版連接地址, topUrl:頂層連接地址, offsetX:x軸偏移量, offsetY:y軸偏移量, width:頂層圖片寬度, height:頂層圖片高度, bucket:oss根目錄]
*/
public static String merge(String blankUrl, String topUrl, int offsetX, int offsetY, int width, int height, String bucket){
try {
return merge(ImageIO.read(new URL(blankUrl)), ImageIO.read(new URL(topUrl)), offsetX, offsetY, width, height, bucket);
} catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* @Description 兩張圖片合併成一張圖片
* @Param [blankFile:底版圖片, topFile:頂層圖片, offsetX:x軸偏移量, offsetY:y軸偏移量, width:頂層圖片寬度, height:頂層圖片高度, bucket:oss根目錄]
*/
public static String merge(File blankFile, File topFile, int offsetX, int offsetY, int width, int height, String bucket){
try {
return merge(ImageIO.read(blankFile), ImageIO.read(topFile), offsetX, offsetY, width, height, bucket);
} catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* @Description 兩張圖片合併成一張圖片
* @Param [blankUrl:底版連接地址, topUrl:頂層連接地址, offsetX:x軸偏移量, offsetY:y軸偏移量, width:頂層圖片寬度, height:頂層圖片高度, bucket:oss根目錄]
*/
public static String merge(URL blankUrl, URL topUrl, int offsetX, int offsetY, int width, int height, String bucket){
try {
return merge(ImageIO.read(blankUrl), ImageIO.read(topUrl), offsetX, offsetY, width, height, bucket);
} catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* @Description 兩張圖片合併成一張圖片
* @Param [blankUrl:底版連接地址, topInputStream:頂層流, offsetX:x軸偏移量, offsetY:y軸偏移量, width:頂層圖片寬度, height:頂層圖片高度, bucket:oss根目錄]
*/
public static String merge(String blankUrl, InputStream topInputStream, int offsetX, int offsetY, int width, int height, String bucket){
try {
return merge(ImageIO.read(new URL(blankUrl)), ImageIO.read(topInputStream), offsetX, offsetY, width, height, bucket);
} catch (Exception e){
e.printStackTrace();
}
return null;
}
public static String merge(BufferedImage blank, BufferedImage top, int offsetX, int offsetY, int width, int height, String bucket){
try {
top = resize(width, height, top);
Graphics g = blank.getGraphics();
g.drawImage(top, blank.getWidth() - top.getWidth() - offsetX, offsetY, null);
g.dispose();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
ImageIO.write(blank, "png", imageOutput);
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
//上傳到oss
//在 oss 上存放位置,文件需要帶後綴,否則阿里雲識別不出來
String imagesPath = "/uploads/qrcode/temporary/" + DigestUtil.getUUID() + ".png";
OssConstants oss = new OssConstants();
oss.simpleUpload(imagesPath, inputStream);
return imagesPath;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* @Description 對圖片進行縮放
* @Param [targetWidth:縮放後的寬度, targetHeight:縮放後的高度, image:圖片]
*/
public static BufferedImage resize(int targetWidth, int targetHeight, BufferedImage image) {
double scaleW = (double) targetWidth / (double) image.getWidth() ;
double scaleH = (double) targetHeight / (double) image.getHeight() ;
double scale = scaleW < scaleH ? scaleW : scaleH;
BufferedImage result = new BufferedImage((int) (image.getWidth() * scale), (int) (image.getHeight() * scale), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = result.createGraphics();
g2d.drawImage(image, 0, 0, result.getWidth(), result.getHeight(), null);
g2d.dispose();
return result;
}
}