好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java实现图片分割指定大小

本文实例为大家分享了java实现图片分割指定大小的具体代码,供大家参考,具体内容如下

1.使用工具:Thumbnails

Thumbnails 是由谷歌提供的图片处理包,目前版本0.4.8。

可以简洁的实现图片的缩放、压缩、旋转、水印、格式转换等操作。

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();

         //大图字节转换

         ByteArrayOutputStream outBig = new ByteArrayOutputStream();

 

         try {

             ImageIO.write(bufferedImageBig, fileSuffix, outBig);

         } catch (IOException e) {

             e.printStackTrace();

         }

         return outBig.toByteArray();

     }

 

}

4.切割图片返回字节数组

?

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数组,即为切割后的文件******************/

        

      } catch (Exception e) {

            System.out.println( "错误" );          

      }

                

                   

     }

小编再为大家补充一段相关代码: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);

             //设置抗锯齿

             g2.drawImage(bi1, 0 , 0 , null );

             g2.dispose();

             ImageIO.write(bi2, "jpg" , new File( "e:/2.jpg" ));

         } catch (IOException e) {

             e.printStackTrace();

         }

     }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/luChenH/article/details/89556258

查看更多关于java实现图片分割指定大小的详细内容...

  阅读:10次