简体   繁体   中英

Convert/display byte array to bmp/jpeg image

First of all, im asking this specific question, because i've already read many examples around this topic, but none of them really helped.

My problem is that i'd like to convert and display a jpg image. I don't have the original image, it is on a server. The data comes through a stream (socket) and it is given in a byte array. In this byte array, every 4 bytes represents information about a pixel. I managed to get the RGBs and convert/save them into a bmp image, however i couldn't find a working solution to make a jpg.

On the other hand, i have a BMP :) how can i display it in a JLabel or JPanel? I also read abou imageicon ImageIO etc. but it doesn't work for me. I use ScheduleExecutorSystem btw. Maybe that is the problem?

Here's the encoder code (Copyright (C) 2013 Philipp C. Heckel ):

public static void encodeToBitmap(byte[] srcBytes, OutputStream destStream)
        throws IOException {

    int imageWidth = 1024;
    int imageHeight = 1080;

    int imageBytes = imageWidth * imageHeight * 3;
    int filesizeBytes = imageBytes + BMP_SIZE_HEADER;

    byte[] header = BMP_HEADER.clone(); // Clone bitmap header template, and
                                        // overwrite with fields

    header = writeIntLE(header, BMP_OFFSET_FILESIZE_BYTES, filesizeBytes);
    header = writeIntLE(header, BMP_OFFSET_IMAGE_WIDTH, imageWidth);
    header = writeIntLE(header, BMP_OFFSET_IMAGE_HEIGHT, imageHeight);
    header = writeIntLE(header, BMP_OFFSET_IMAGE_DATA_BYTES, 0);
    header = writeIntLE(header, BMP_OFFSET_PAYLOAD_LENGTH,
            (int) srcBytes.length);

    // WRITE TO STREAM

    // Add payload

    destStream.write(header, 0, header.length);

    for (int offset = imageBytes - imageWidth * 3; offset >= 0; offset -= imageWidth * 3) {

        for (int i = 0; i < (imageWidth) * 3; i++) {
            destStream.write(srcBytes[offset + i]);
        }
    }

    destStream.close();
}

And the code, how i use it:

BitmapEncoder.encodeToBitmap(RGBvalues, new FileOutputStream("path to file"));
RGBvalues - bytes of 3, with the RGB values (i don't say!! :P )

image width and Height is fix for debug purpose

I have a JPanel and a JLabel within, and i added a default ImageIcon to the JLabel (a jpg image), and it works fine. After i saved the image, i try :

ImageIcon icon = new ImageIcon("path to file");
pictureLabel.setIcon(icon);

It only makes the default disappear. Am i forgetting to set something? Is this the fastest way, to do it?

The ImageIcon class only directly supports reading PNG, GIF and JPEG types. You need to use ImageIO to read your bitmap into a generic Image object (which is supported by ImageIcon ) and then pass that to ImageIcon . Here's how I'd modify your code to work:

File imgFile = new File("path to file");
Image image = javax.imageio.ImageIO.read(imgFile);
ImageIcon icon = new ImageIcon(image);
pictureLabel.setIcon(icon);

Note that you can also use ImageIO to convert your bitmap to JPEG format.

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