代码如下:
java.net.URL url = new URL("图片下载地址"); BufferedImage imageInfo = ImageIO.read(url);//read也可以直接读本地文件 Graphics g = imageInfo.getGraphics(); g.setColor(Color.RED);//画笔红色 BasicStroke stokeLine = new BasicStroke(3.0f); ((Graphics2D) g).setStroke(stokeLine); g.drawRect(new BigDecimal(imageInfo.getWidth()*0.58723885).intValue(), new BigDecimal(imageInfo.getHeight()*0.57600826).intValue(), new BigDecimal(imageInfo.getWidth()*0.10220215).intValue(), new BigDecimal(imageInfo.getHeight()*0.1106515).intValue()); g.setColor(Color.ORANGE);//画笔橘黄色 g.drawRect(new BigDecimal(imageInfo.getWidth()*0.4217956).intValue(), new BigDecimal(imageInfo.getHeight()*0.48810756).intValue(), new BigDecimal(imageInfo.getWidth()*0.036137775).intValue(), new BigDecimal(imageInfo.getHeight()*0.0630817).intValue()); FileOutputStream out = new FileOutputStream("d:\\test5.jpg");//输出图片的地址 ImageIO.write(imageInfo, "jpeg", out); /** * base64转化开始 */ ByteArrayOutputStream stream = new ByteArrayOutputStream(); ImageIO.write(imageInfo, "jpeg", stream); BASE64Encoder encoder = new BASE64Encoder(); LOGGER.error(encoder.encode(stream.toByteArray())); stream.close();