简体   繁体   中英

Converting byte[] array to BufferedImage object

I have taken an image into BufferedImage object. Now I have converted the array to a byte array. I wanted to modify the byte data and then print the new Image. This conversion is giving my null object in BufferedImage. Also please tell me how image is stored in single dimentional byte array because according to me its size for a 3*3 image must be near around 9 but to shock it is: 672bytes

Please help me.

I have tried googling. Saw other people's code.

import java.io.*;
import javax.imageio.ImageIO;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
class SVisualizer{
    JFrame f;
    JLabel l;
    SVisualizer(){
        f=new JFrame("SVisualizer");
        f.setLayout(new FlowLayout());
        f.setLocationRelativeTo(null);
        f.setSize(590, 428);
        f.setResizable(false);
        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);    
    }
    public byte[] inputImage(){
        try{
        File file=new File("C:/Users/sanss/OneDrive/Desktop/1.jpg");
        BufferedImage bufferedImage = ImageIO.read(file);
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpg", baos);
        return baos.toByteArray();
        }
        catch(Exception e){
            System.out.println("Image cannot be processed!?");
        }
        return null;
    }
    public BufferedImage outputImage(byte[]data){
        try{
         ByteArrayInputStream bis = new ByteArrayInputStream(data);
         BufferedImage bImage = ImageIO.read(bis);
         return bImage;
        }
        catch(Exception e){
            System.out.println("Error while parsing array!?");
        }
        return null;
    }
    public int[] setWeight(int n){
        int[] data=new int[n];
        for(int i=0;i<n;i++)
            data[i]=i;
        return data;
    }
    public void setRandom(byte[] data, int[] weight){
        //back to front and front to back, simple randomisation
        int n=data.length;
        for(int i=0;i<n/2;i++){
            byte temp1=data[i];
            int temp2=weight[i];
            data[i]=data[n-1-i];
            weight[i]=data[n-1-i];
            data[n-i-1]=temp1;
            weight[n-i-1]=temp2;
        }
    }
    public void setImage(BufferedImage bImage){
        ImageIcon imageIcon=null;
        try{
        imageIcon=new ImageIcon(bImage);
        f.setContentPane(new JLabel(imageIcon));
        f.setVisible(true);
        }
        catch(Exception e){
            System.out.println("Error in setImage");
            System.out.println("BufferedImage: "+bImage);
            System.out.println("ImageIcon: "+imageIcon);
        }
    }
    public void printByteArray(byte[]data){
        for(int i=0;i<data.length;i++)
            System.out.println(data[i]+" ");
    }
    /*
    public void bubbleSort(byte[] data, int[] weight){
        int n=data.length;
        BufferedImage finalImage=outputImage(data);
        for(int i=0;i<n;i++){
            for(int j=0;j<n-1-i;j++){

            }
        }
    }*/
    public static void main(String args[]){
        SVisualizer ob=new SVisualizer();
        byte[] array=ob.inputImage();
        int[] weight=ob.setWeight(array.length);
        ob.setRandom(array, weight);
        BufferedImage oImage=ob.outputImage(array);
        ob.setImage(oImage);
        //ob.printByteArray(array);
        System.out.println(array.length);
    }
}

The bytes of a JPEG are not pixels. JPEG is a compressed (and usually lossy) format. Modifying bytes directly will corrupt the compressed data, resulting in a noncompliant file that is no longer a valid JPEG image.

To convert image pixels to a byte array, you will need to read the raw pixel data from your BufferedImage. To do that, you first have to decide what each byte value will mean. A common approach is to use four bytes to store each pixel: one for the red component of the pixel color, one for the green component, one for the blue component, and one for the alpha (transparency) component. This is typically abbreviated RGBA.

Many other configurations are possible. For instance, if your image is grayscale, you can simply store each pixel's gray value in one byte.

If we assume the first approach, you can use the getRGB method of BufferedImage to retrieve the values:

int width = bImage.getWidth();
int height = bImage.getHeight();

byte[] bytes = new byte[width * height * 4];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
IntBuffer intBuffer = buffer.asIntBuffer();

int[] rowPixels = new int[width];
for (int y = 0; y < height; y++) {
    bImage.getRGB(0, y, width, 1, rowPixels, 0, width);
    intBuffer.put(rowPixels);
}

return bytes;

You can use setRGB to convert the bytes back to an image:

ByteBuffer buffer = ByteBuffer.wrap(data);
IntBuffer intBuffer = buffer.asIntBuffer();

BufferedImage bImage = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_ARGB);

int[] rowPixels = new int[width];
for (int y = 0; y < height; y++) {
    intBuffer.get(rowPixels);
    bImage.setRGB(0, y, width, 1, rowPixels, 0, width);
}

As for how a one-dimensional array holds pixels: an image format will specify how. Typically, each row of pixels occurs in the array right after the preceding row. The exact number of bytes between the start of each row within the array is known as the scanline stride.

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