0

I'm trying to use the PictureService in Gluon Mobile to get a profile picture that can be stored in a MySQL table (Blob). What is the best way to get the byte array (and if possible the base64 encoded string) from the returned image object of the service? I tried SwingFXUtils, but it caused the app to crash on my Android. Thus, I am not sure the SwingFXUtils is supported.

    Services.get(PicturesService.class).ifPresent(service -> {
            service.takePhoto(false).ifPresent(image -> {
                try {
                    byte[] imageStream = PictureFactory.convertToByteArray(image);
                    //String base64String = Base64.getEncoder().encodeToString(imageStream);
                    //this.picModel.setByteArray(imageStream);
                    //this.picModel.setBase64Str(base64String);
                    //this.picModel.setPatientId(User.patient.getPatientId());

                    av.setImage(image); //Avatar
                    av.setRotate(-90);

                    if (this.imageBox.getChildren().size() < 3) {
                        this.imageBox.getChildren().add(0, av);
                    }


                } catch (Exception ex) {
                    Logger.getLogger(PictureFactory.class.getName()).log(Level.SEVERE, null, ex);
                }
            });
        });

Byte Array Conversion Code:

public static byte[] convertToByteArray(Image img) throws IOException {
    BufferedImage byteImg = SwingFXUtils.fromFXImage(img, null);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ImageIO.write(byteImg, "png", stream);
    byte[] result = stream.toByteArray();
    stream.close();
    return result;
}

A completely unrelated issue which I am also experiencing is that the image is rotated 90 degrees clockwise when taking it in portrait mode. Any way to fix this? I'm hoping it can be fixed in the image itself that gets sent to the database instead of just rotating the imageview.

A.Sharma
  • 2,771
  • 1
  • 11
  • 24
  • There are already a few questions about this: [here](https://stackoverflow.com/questions/42038942/getpublicstoragepictures-lists-no-files/42052139#42052139) you will find a way to implement a method to encode the image into a string, that you can decode later on. – José Pereda Nov 02 '17 at 17:59
  • Thank you Jose. That was able to resolve it. – A.Sharma Nov 06 '17 at 01:24

0 Answers0