简体   繁体   English

如何从JLabel图标获取ByteArray?

[英]How to get ByteArray from JLabel icon?

I'm trying to get ByteArray from icon of JLabel and then store it to my database. 我正在尝试从JLabel图标获取ByteArray ,然后将其存储到我的数据库中。
The reason being because when I open image in my JLabel it gets resized to 300x300 pixels, so I want to save that resized picture to my database, to keep my database lightweight. 原因是因为当我在JLabel打开图像时,图像的大小调整为300x300像素,因此我想将调整后的图像保存到数据库中,以保持数据库的轻量化。
I want to do that 'on the fly' without having to save that resized picture on the disk. 我想“即时”执行此操作,而不必将调整大小后的图片保存在磁盘上。

I have no problem to convert the files from the disk to a ByteArray and storing those ByteArrays in a SQLite database. 我将磁盘上的文件转换为ByteArray并将这些ByteArrays存储在SQLite数据库中没有问题。
But getting the ByteArray from an icon of a JLabel is a mission impossible for me. 但是从JLabel的图标获取ByteArray对我来说是不可能完成的任务。
I don't know is it even possible. 我不知道有没有可能。

So help me out, guys. 伙计们,请帮帮我。
Is there any way to do that? 有什么办法吗?

The JLabel is simply a placeholder for the image, you are still using the image object. JLabel只是图像的占位符,您仍在使用图像对象。 Wouldn't it be easier to simply convert the image into a byte array? 简单地将图像转换为字节数组会更容易吗?

File f = new File("img.jpg");
BufferedImage image = ImageIO.read(fnew);
ByteArrayOutputStream b =new ByteArrayOutputStream();
ImageIO.write(image, "jpg", b );
byte[] imageInByte = b.toByteArray();

Or if you wanted to retrieve the image from a JLabel you can use: 或者,如果您想从JLabel检索图像,则可以使用:

Icon icon = label.getIcon();
BufferedImage image = new BufferedImage(icon.getIconWidth(),
            icon.getIconHeight(),BufferedImage.TYPE_INT_RGB);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM