本文實(shí)例為大家分享了java實(shí)現(xiàn)圖片分割指定大小的具體代碼,供大家參考,具體內(nèi)容如下
1.使用工具:Thumbnails
Thumbnails 是由谷歌提供的圖片處理包,目前版本0.4.8。
可以簡潔的實(shí)現(xiàn)圖片的縮放、壓縮、旋轉(zhuǎn)、水印、格式轉(zhuǎn)換等操作。
2.引入maven
1
2
3
4
5
6
|
< dependency > < groupId >net.coobird</ groupId > < artifactId >thumbnailator</ artifactId > < version >0.4.8</ version > </ dependency > //最新版本可自查 |
3.工具類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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ù)組
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/** * 接收文件 * * * @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圖片切割圓形
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@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í)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/luChenH/article/details/89556258