Pergunta

I have a question about why i am getting the outputs i am on this code. I have a bigger project i am working on but I narrowed the problem down to this. Here is the code:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.imageio.ImageIO;

public class Main {

static String workingDirectory;

static int num1;
static int num2;
static BufferedImage image1;
static BufferedImage image2;

public static final int JOB = 0;

@SuppressWarnings("unused")
public static void main(String[] args) {

    workingDirectory = System.getProperty("user.dir") + "/src/";

    if(JOB == 0){
        read(workingDirectory + "output&input.myExtention");
    }else if(JOB == 1){
        num1 = 18;
        num2 = 100;
        getImages("Smile.jpeg", "Frown.jpeg");
        write(workingDirectory + "output&input.myEx");
    }

}

static void getImages(String imageName1, String imageName2){
    try{
        image1 = ImageIO.read(new File(workingDirectory + imageName1));
        image2 = ImageIO.read(new File(workingDirectory + imageName2));
    }catch(IOException e){
        e.printStackTrace();
    }
}

static void read(String path){
    File file = new File(path);

    try {
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);

        num1 = ois.readInt();
        System.out.println("Num1 = " + num1);

        image1 = ImageIO.read(ois);
        System.out.println("Image1 = " + image1);

        num2 = ois.readInt();
        System.out.println("Num2 = " + num2);

        image2 = ImageIO.read(ois);
        System.out.println("Image2 = " + image2);

        ois.close();
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

static void write(String path){
    File file = new File(path);

    try{
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        System.out.println("Writing Num1 = " + num1);
        oos.writeInt(num1);
        System.out.println("Image1 = " + image1);
        ImageIO.write(image1, "png", oos);
        System.out.println("Writing Num2 = " + num2);
        oos.writeInt(num2);
        System.out.println("Image2 = " + image2);
        ImageIO.write(image2, "png", oos);

        oos.close();
        fos.close();
    }catch(FileNotFoundException e){
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
}
}

And here is the output on two different runs. The first I set JOB = 1 (writing the file) and for the second I set JOB = 0 (reading the file).

OUTPUT 1:

Writing Num1 = 18
Image1 = BufferedImage@77827284: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@152c7568 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 225 height = 225 #numDataElements 3 dataOff[0] = 2
Writing Num2 = 100
Image2 = BufferedImage@16ba8602: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@152c7568 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 225 height = 225 #numDataElements 3 dataOff[0] = 2


OUTPUT 2:

Num1 = 18
Image1 = BufferedImage@4d865b28: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@2fcac6db transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 225 height = 225 #numDataElements 3 dataOff[0] = 2
Num2 = -1974494307
Image2 = null

What I'm wondering is why I am getting different readings than what I wrote and why do images read as null after the first one?

Foi útil?

Solução

A png is not a Java java.lang.Object (e.g. a raw byte sequence doesn't encode a length and byte arrays are not dynamically sized in Java they are java.lang.Objects). Therefore, one solution is to wrap your (ImageIO.write(image1, "png", oos); [and the second image write] and then the read calls) with a byte[]. Allow me to illustrate, that is something like -

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image1, "png", baos); 
oos.writeObject(baos.toByteArray());
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
ImageIO.write(image2, "png", baos2); 
oos.writeObject(baos2.toByteArray());

and then update your read (that is) -

image1 = ImageIO.read(new ByteArrayInputStream((byte[])ois.readObject()));
image2 = ImageIO.read(new ByteArrayInputStream((byte[])ois.readObject()));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top