[英]Java JFileChooser .png files to byte[] black image
I need to upload an image .png to the server and show it to Swing GUI, I am using JFileChooser, the user selects a .png image and it gets stored on a byte[]. 我需要将图像.png上载到服务器并将其显示到Swing GUI,我使用的是JFileChooser,用户选择一个.png图像并将其存储在byte []中。 Later when I try to display the image, it is all in black.
稍后,当我尝试显示图像时,图像全为黑色。 This don't happen with .jpg files.
.jpg文件不会发生这种情况。 I have a problem with the transparency of .png images.
我对.png图像的透明度有疑问。 This is my code:
这是我的代码:
Saving Image to byte[]: 将图像保存到字节[]:
public byte[] AvatarToByte(String url){
byte[] data = null;
String extension = "";
try{
BufferedImage bImage = ImageIO.read(new File(url));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if(url.toString().toLowerCase().contains(".png")){
extension = "png";
}else{
extension = "jpg";
}
ImageIO.write(bImage, extension, bos );
data = bos.toByteArray();
}catch(Exception e){
e.printStackTrace();
}
return data;
}
From byte[] to java swing: 从byte []到Java swing:
public ByteToAvatar(byte[] data){
BufferedImage img = null;
img = ImageIO.read(new ByteArrayInputStream(data));
JLabel lblURL = new JLabel();
lblURL.setBorder(new LineBorder(Color.GRAY));
lblURL.setBackground(SystemColor.controlLtHighlight);
lblURL.setBounds(10, 10, 80, 80);
lblURL.setIcon(Resize(img));
}
Resize: 调整:
private ImageIcon Resize(Image img){
Image newImg = img.getScaledInstance(80, 80, Image.SCALE_SMOOTH);
ImageIcon image = new ImageIcon(newImg);
return image;
}
edit: tried to display the image before resizing and it is shown correctly. 编辑:尝试在调整大小之前显示图像,并且显示正确。 There must be something wrong with Resize(Image img).
Resize(Image img)一定有问题。
I have tested your code and it works without problems. 我已经测试过您的代码,并且可以正常工作。
import java.awt.Color; import java.awt.GraphicsConfiguration; import java.awt.Image; import java.awt.SystemColor; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.LineBorder; public class Test { static GraphicsConfiguration gc; static JFrame frame; public static void main(String[] args) throws IOException { // TODO Auto-generated method stub frame= new JFrame(gc); frame.setSize(200, 200); frame.setVisible(true); byte[] b= AvatarToByte("e:/dog.png"); ByteToAvatar(b); System.out.println(); } public static byte[] AvatarToByte(String url){ byte[] data = null; String extension = ""; try{ BufferedImage bImage = ImageIO.read(new File(url)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); if(url.toString().toLowerCase().contains(".png")){ extension = "png"; }else{ extension = "jpg"; } ImageIO.write(bImage, extension, bos ); data = bos.toByteArray(); }catch(Exception e){ e.printStackTrace(); } return data; } public static void ByteToAvatar(byte[] data) throws IOException{ BufferedImage img = null; img = ImageIO.read(new ByteArrayInputStream(data)); JLabel lblURL = new JLabel(); lblURL.setBorder(new LineBorder(Color.GRAY)); lblURL.setBackground(SystemColor.controlLtHighlight); lblURL.setBounds(10, 10, 80, 80); lblURL.setIcon(Resize(img)); frame.getContentPane().add(lblURL); frame.pack(); } private static ImageIcon Resize(Image img){ Image newImg = img.getScaledInstance(80, 80, Image.SCALE_SMOOTH); ImageIcon image = new ImageIcon(newImg); return image; } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.