java實(shí)現(xiàn)圖片分割指定大小
本文實(shí)例為大家分享了java實(shí)現(xiàn)圖片分割指定大小的具體代碼,供大家參考,具體內(nèi)容如下
1.使用工具:Thumbnails
Thumbnails 是由谷歌提供的圖片處理包,目前版本0.4.8。
可以簡潔的實(shí)現(xiàn)圖片的縮放、壓縮、旋轉(zhuǎn)、水印、格式轉(zhuǎn)換等操作。
2.引入maven
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
//最新版本可自查
3.工具類
import org.springframework.web.multipart.MultipartFile;
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* @Auther: lch
* @Date: 2019/3/11 09:58
* @Description: 圖片工具類
*/
public class ImgUtils {
public static byte[] uploadImg(Integer height,Integer width,MultipartFile file) throws Exception{
String fileSuffix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
BufferedImage bufferedImageBig = Thumbnails.of(file.getInputStream())
.forceSize(height, width).asBufferedImage();
//大圖字節(jié)轉(zhuǎn)換
ByteArrayOutputStream outBig = new ByteArrayOutputStream();
try {
ImageIO.write(bufferedImageBig, fileSuffix, outBig);
} catch (IOException e) {
e.printStackTrace();
}
return outBig.toByteArray();
}
}
4.切割圖片返回字節(jié)數(shù)組
/**
* 接收文件
*
*
* @param model
* @return
* @throws IOException
* @throws IllegalStateException
*/
@RequestMapping(value = "imageupload")
public void imageUpload(MultipartFile file) throws IllegalStateException, IOException {
//文件名稱
String realFileName = file.getOriginalFilename();
//文件后綴
String suffix = realFileName.substring(realFileName.lastIndexOf(".") + 1);
/***************文件處理*********************/
try {
//大圖圖片切割 --寬高 720 - 720
byte[] bytesBig = ImgUtils.uploadImg(720, 720, file);
//中圖圖片切割 --寬高 200 - 200
byte[] bytesMiddle = ImgUtils.uploadImg(200, 200, file);
//小圖圖片切割 --寬高 50- 50
byte[] bytesSmall = ImgUtils.uploadImg(50, 50, file);
/************以上三種byte數(shù)組,即為切割后的文件******************/
} catch (Exception e) {
System.out.println("錯(cuò)誤");
}
}
小編再為大家補(bǔ)充一段相關(guān)代碼:java圖片切割圓形
@Test
public void test() {
try {
// 讀取圖片
BufferedImage bi1 = ImageIO.read(new File("g:/free-sheet-share.jpg"));
BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(),
BufferedImage.TYPE_INT_RGB);
Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1
.getHeight());
Graphics2D g2 = bi2.createGraphics();
g2.setBackground(Color.WHITE);
g2.fill(new Rectangle(bi2.getWidth(), bi2.getHeight()));
g2.setClip(shape);
//設(shè)置抗鋸齒
g2.drawImage(bi1, 0, 0, null);
g2.dispose();
ImageIO.write(bi2, "jpg", new File("e:/2.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java倒計(jì)時(shí)三種實(shí)現(xiàn)方式代碼實(shí)例
這篇文章主要介紹了Java倒計(jì)時(shí)三種實(shí)現(xiàn)方式代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
使用Springboot自定義轉(zhuǎn)換器實(shí)現(xiàn)參數(shù)去空格功能
這篇文章主要介紹了使用Springboot自定義轉(zhuǎn)換器實(shí)現(xiàn)參數(shù)去空格功能,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring?MVC?請求映射路徑的配置實(shí)現(xiàn)前后端交互
在Spring?MVC中,請求映射路徑是指與特定的請求處理方法關(guān)聯(lián)的URL路徑,這篇文章主要介紹了Spring?MVC?請求映射路徑的配置,實(shí)現(xiàn)前后端交互,需要的朋友可以參考下2023-09-09
springboot集成JWT實(shí)現(xiàn)身份認(rèn)證(權(quán)鑒)的方法步驟
本文主要介紹了springboot集成JWT實(shí)現(xiàn)身份認(rèn)證(權(quán)鑒)的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

