java base64图片截图

需求:接收到其他服务发送过来的base64的图片,然后给了个截图的4个点的坐标,现在需要根据4个点的坐标,进行截图之后返回到前端去显示出来;主要使用BufferedImage的getSubimage方法;如下所示

1>构造个对象封装下这几个参数,从其他服务中接受到数据格式为图片的四个点坐标的一个二维数组,需要转换为截图的点x坐标,y坐标,截图的宽度和高度;所以先封装个对象;通过一个二维数组计算出这些参数:

 class ImageInfo {
        private int x;
        private int y;
        private int width;
        private int height;
        public ImageInfo(String[][] position) {
            String[] strings = position[0];
            int x = Integer.parseInt(strings[0]);
            int y = Integer.parseInt(strings[1]);
            String[] forthPoint = position[2];
            int x1 = Integer.parseInt(forthPoint[0]);
            int y1 = Integer.parseInt(forthPoint[1]);
            this.x = x;
            this.y = y;
            this.width = x1 - x;
            this.height = y1 - y;
        }
}

2>转换的主要代码

public String getSubImage(String sourceImage, String[][] positions) throws IOException {
        ImageInfo imageInfo = new ImageInfo(positions);
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = decoder.decodeBuffer(sourceImage);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(b);
        BufferedImage bi = ImageIO.read(inputStream);
        BufferedImage subimage = bi.getSubimage(imageInfo.x, imageInfo.y, imageInfo.width, imageInfo.height);
        return bufferImageToBase64(subimage);
    }

主要构造一个BufferedImage对象,然后调用getSubimage方法,结果返回一个BufferedImage对象;最后再把BufferedImage转换成base64字符串返回;

3>bufferImageToBase64方法

private String bufferImageToBase64(BufferedImage bufferedImage) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpg", baos);
        byte[] bytes = baos.toByteArray();
        String trim = encoder.encodeBuffer(bytes).trim();
        return trim.replaceAll("\n", "").replaceAll("\r", "");
    }

最后要注意的是,最后要进行空格字符的替换;不然显示不正确的;

4>encoder对象主要用来解码使用;

static BASE64Encoder encoder = new sun.misc.BASE64Encoder();5>前端显示使用下面格式,然后后面跟上返回的内容;
<img src="data:image/jpeg;base64,
Java中的Base64图片处理是通过将图片转换为Base64编码的字符串来实现的。使用JavaBase64类,可以将图片文件转换为Base64编码的字符串,也可以将Base64编码的字符串转换为图片文件。 下面是一个使用Java处理图片实现Base64编码转换的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Base64; public class Base64Image { public static void main(String[] args) { String imagePath = "path/to/image.jpg"; // 将图片文件转换为Base64编码的字符串 String base64Image = encodeImage(imagePath); System.out.println("Base64编码的图片字符串: " + base64Image); // 将Base64编码的字符串转换为图片文件 String decodedImagePath = "path/to/decoded_image.jpg"; decodeImage(base64Image, decodedImagePath); System.out.println("已将Base64编码的字符串转换为图片文件: " + decodedImagePath); } private static String encodeImage(String imagePath) { File imageFile = new File(imagePath); try (FileInputStream fis = new FileInputStream(imageFile)) { byte[] imageBytes = new byte[(int) imageFile.length()]; fis.read(imageBytes); return Base64.getEncoder().encodeToString(imageBytes); } catch (IOException e) { e.printStackTrace(); } return null; } private static void decodeImage(String base64Image, String decodedImagePath) { byte[] imageBytes = Base64.getDecoder().decode(base64Image); try { FileUtils.writeByteArrayToFile(new File(decodedImagePath), imageBytes); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码中的`encodeImage`方法将图片文件转换为Base64编码的字符串,而`decodeImage`方法则将Base64编码的字符串转换为图片文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值