/**
* base64 編碼轉(zhuǎn)換為 BufferedImage
*
* @param base64 不帶前綴以9j/開頭的
* @return BufferedImage對(duì)象
*/
public static BufferedImage base64ToBufferedImage(String base64) {
Base64.Decoder decoder = Base64.getDecoder();
try {
byte[] bytes = decoder.decode(base64);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
return ImageIO.read(byteArrayInputStream);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 添加邊框
*
* @param image base64ToBufferedImage轉(zhuǎn)換出來的image對(duì)象
* @param x 原點(diǎn)x坐標(biāo)(原點(diǎn)一般在左上角)
* @param y 原點(diǎn)y坐標(biāo)(原點(diǎn)一般在左上角)
* @param width 框?qū)? * @param height 框高
* @return
*/
public static BufferedImage addBorders(BufferedImage image, int x, int y, int width, int height) {
if (image == null) {
return null;
}
Graphics2D g2 = image.createGraphics();
//畫筆顏色
g2.setColor(Color.RED);
//設(shè)置畫筆粗細(xì)
BasicStroke stokeLine = new BasicStroke(8.0f);
g2.setStroke(stokeLine);
//矩形框(原點(diǎn)x坐標(biāo),原點(diǎn)y坐標(biāo),矩形的長,矩形的寬)
g2.drawRect(x, y, width, height);
g2.dispose();
return image;
}
/**
* BufferedImage轉(zhuǎn)換為 MultipartFile
*
* @param image BufferedImage對(duì)象
* @return MultipartFile對(duì)象
*/
public static MultipartFile imageToMultipartFile(BufferedImage image) {
try {
//BufferedImage 轉(zhuǎn)化為 ByteArrayOutputStream
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", out);
//ByteArrayOutputStream 轉(zhuǎn)化為 byte[]
byte[] imageByte = out.toByteArray();
//將 byte[] 轉(zhuǎn)為自定義的 MultipartFile對(duì)象(實(shí)現(xiàn)MultipartFile接口)
return new CustomMultipartFile(imageByte, "/9j");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
// base64轉(zhuǎn)為image
BufferedImage image = ImageUtils.base64ToBufferedImage("9j........");
// 畫框
ImageUtils.addBorders(image, 11, 12, 45, 72);
// image 轉(zhuǎn)為 MultipartFile
MultipartFile multipartFile = ImageUtils.imageToMultipartFile(image);
//畫框后的圖片保存到Minio中
String url = MinioUtil.uploadFileMinio(multipartFile, "fileFormat");
}
Java圖片畫框,算法標(biāo)記
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 由于最近的Java作業(yè)要求將圖片放大縮小,主要就是選用一種插值算法,如最鄰近插值、雙線性二次插值、雙線性三次插值,...
- 前言 最近公司孵化一個(gè)項(xiàng)目,里面涉及到大量的原創(chuàng)照片及圖片,目前我們使用的明文水印,不但對(duì)用戶體驗(yàn)有一定影響,并且...
- FuzzyDetection 圖片擇優(yōu)(選擇最清楚的圖片)Java實(shí)現(xiàn) 效果不錯(cuò) 大家可以自己改成自己喜歡的語言 ...
- 先上效果圖 1.為了實(shí)現(xiàn)圖片的放到縮小,我選擇了PhotoView 框架用于顯示圖片。 2.使用Glide 框架加...