java实现图片分割指定大小

来自:网络
时间:2021-10-02
阅读:

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

1.使用工具:Thumbnails

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

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

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();
        //大图字节转换
        ByteArrayOutputStream outBig = new ByteArrayOutputStream();
 
        try {
            ImageIO.write(bufferedImageBig, fileSuffix, outBig);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outBig.toByteArray();
    }
 
}

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

/**
* 接收文件
* 
* 
* @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图片切割圆形

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

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

返回顶部
顶部