String soursePicPath = "src/main/resources/static/images/units.png";
File sourcePic = new File(soursePicPath);
try {
BufferedImage pic1 = ImageIO.read(sourcePic);
int width = pic1.getWidth();
int height = pic1.getHeight();
//参数依次为,截取起点的x坐标,y坐标,截取宽度,截取高度
BufferedImage pic2 = pic1.getSubimage(1, param1, 113, 36);
//将截取的子图另行存储
File desImage = new File("C:/Users/Administrator.DESKTOP-V7L9RPR/Desktop/123/ui/"+System.currentTimeMillis()+".png");
ImageIO.write(pic2, "png", desImage);
System.out.println(desImage);
} catch (IOException e1) {
e1.printStackTrace();
}
怎么把地址转为Base64
//怎么把地址转为Base64
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(pic2, "png", stream);
String s = Base64.getEncoder().encodeToString(stream.toByteArray());
在上面的代码把下列注释掉换成上述代码
// File desImage = new File("C:/Users/Administrator.DESKTOP-
// V7L9RPR/Desktop/123/ui/"+System.currentTimeMillis()+".png");
// ImageIO.write(pic2, "png", desImage);