需求:接收到其他服务发送过来的base64的图片,然后给了个截图的4个点的坐标,现在需要根据4个点的坐标,进行截图之后返回到前端去显示出来;主要使用BufferedImage的getSubimage方法;如下所示
/**
* Returns a subimage defined by a specified rectangular region.
* The returned <code>BufferedImage</code> shares the same
* data array as the original image.
* @param x the X coordinate of the upper-left corner of the
* specified rectangular region
* @param y the Y coordinate of the upper-left corner of the
* specified rectangular region
* @param w the width of the specified rectangular region
* @param h the height of the specified rectangular region
* @return a <code>BufferedImage</code> that is the subimage of this
* <code>BufferedImage</code>.
* @exception RasterFormatException if the specified
* area is not contained within this <code>BufferedImage</code>.
*/
public BufferedImage getSubimage (int x, int y, int w, int h) {
return new BufferedImage (colorModel,
raster.createWritableChild(x, y, w, h,
0, 0, null),
colorModel.isAlphaPremultiplied(),
properties);
}
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[1];
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>转换的主要代码
/**
* 截取字符串
*
* @param sourceImage 源图片
* @param positions 截取坐标
* @return
* @throws IOException
*/
public String getSubImage(String sourceImage, String[][] positions) throws IOException {
ImageInfo imageInfo = new ImageInfo(positions);
// 读取图片字节数组
InputStream in = new FileInputStream(sourceImage);
byte[] data = new byte[in.available()];
in.read(data);
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
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方法
/**
* 将截取后的图片加密为base64,并且去掉换行和空格字符串
*
* @param bufferedImage
* @return
* @throws IOException
*/
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字符串"/>
6>测试
public static void main(String[] args) throws IOException {
Base64Utils base64Utils = new Base64Utils();
// 定义二维数据,输入截取坐标
String[][] positions = new String[2][2];
positions[0][0] = new String(String.valueOf(0));
positions[0][1] = new String(String.valueOf(0));
positions[1][0] = new String(String.valueOf(600));
positions[1][1] = new String(String.valueOf(200));
// 将截取后的图片加密为base64字符串
String base64UtilsSubImage = base64Utils.getSubImage("D://下载/测试照片.jpg", positions);
System.out.println(base64UtilsSubImage);
// 将加密后的字符串解码为图片
Base64Utils.GenerateImage(base64UtilsSubImage, "D://photos/截取后的照片.jpg");
}