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.