简体   繁体   中英

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[]. Later when I try to display the image, it is all in black. This don't happen with .jpg files. I have a problem with the transparency of .png images. 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:

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).

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; } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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