使用java在网络图片上画框并转化为base64

代码如下:

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();
实现这个功能需要使用Python和一些相关的库,包括Qt和OpenCV。以下是一个简单的代码示例,可以在QLABEL上显示图像并绘制矩形框: ```python from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen from PyQt5.QtCore import Qt, QPoint, QRect from PyQt5.QtWidgets import QLabel import cv2 class ImageLabel(QLabel): def __init__(self): super().__init__() self.image = None self.rect_start = None self.rect_end = None def set_image(self, image_path): self.image = cv2.imread(image_path) self.display_image() def display_image(self): height, width, channel = self.image.shape bytesPerLine = 3 * width qImg = QImage(self.image.data, width, height, bytesPerLine, QImage.Format_RGB888).rgbSwapped() pixmap = QPixmap.fromImage(qImg) self.setPixmap(pixmap) def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.rect_start = event.pos() def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton: self.rect_end = event.pos() self.update() def paintEvent(self, event): super().paintEvent(event) if self.rect_start and self.rect_end: painter = QPainter(self) painter.setPen(QPen(Qt.red, 2, Qt.SolidLine)) rect = QRect(self.rect_start, self.rect_end) painter.drawRect(rect) if __name__ == '__main__': import sys from PyQt5.QtWidgets import QApplication app = QApplication(sys.argv) label = ImageLabel() label.set_image('image.jpg') label.show() sys.exit(app.exec_()) ``` 在这个例子中,我们创建了一个名为`ImageLabel`的自定义QLabel类。我们重写了`set_image`方法来读取并显示图像,重写了`mousePressEvent`和`mouseReleaseEvent`方法来记录框的起点和终点,重写了`paintEvent`方法来绘制矩形框。 当我们运行这个代码示例时,它将显示一个名为`image.jpg`的图像,并且我们可以在图像上单击并拖动鼠标来绘制矩形框。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值