I am currently struggling with getting a picture from a camera connected to the uart circuit of the raspberry pi. I am trying to do so with the aid of pi4j. Since I was able to initialize the camera, I do not think that the problem is related to the commands I am sending. However, when I try to open the generated .jpg the file is corrupt.
Has anyone an idea what I am doing wrong or has managed to get a picture form a camera connected to uart of the raspberry pi with java?
Camera: Grove - Serial Camera Kit
Datasheet: Grove - Serial Camera Datasheet PDF
Example Code: Python Code
    private void getPicture(long pictureLength) {
        try {
            byte[] receiveDataPackageCommand = { (byte) 0xaa, (byte) 0x0e, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00 };
            byte[] ackPackageEndCommand = { (byte) 0xaa, (byte) 0x0e, (byte) 0x00, (byte) 0x00, (byte) 0xf0,
                    (byte) 0xF0 };
            File picture = new File(getFileName());
            console.println("created file " + picture.getName());
            if (picture.createNewFile()) {
                FileOutputStream stream = new FileOutputStream(picture.getName());
                int i = 0;
                while (pictureLength > 0) {
                    receiveDataPackageCommand[4] = (byte) (i & 0xff);
                    receiveDataPackageCommand[5] = (byte) ((i >> 8) & 0xff);
                    serial.write(receiveDataPackageCommand);
                    byte[] bytes = pictureLength >= 128 ? serial.read(128) : serial.read((int) pictureLength);
                    stream.write(bytes);
                    pictureLength = pictureLength - 128;
                    i++;
                }
                stream.close();
                serial.write(ackPackageEndCommand);
                console.println("picture received and saved");
            } else {
                console.println("file already exists");
            }
        } catch (Exception ex) {
            console.println(ex);
        }
    }