I have a Pi 3 with the Java SDK added as recommended elsewhere on this forum.
It does not support Java Swing for image conversion so another solution is required for saving image files.
My solution below creates a java buffered image and copies the colour data to it. The resulting buffered image is then saved.
It does not support Java Swing for image conversion so another solution is required for saving image files.
My solution below creates a java buffered image and copies the colour data to it. The resulting buffered image is then saved.
B4X:
Sub Class_Globals
'Private fx As JFX
Private nativeMe,bufImg As JavaObject
End Sub
'Initializes the object. (No parameters)
Public Sub Initialize
nativeMe = Me
End Sub
'Saves fx image to file of type .jpg, .png, bmp, or .gif to
' fname in defined directory
Sub SaveImage(im As Image,dir As String,fname As String)
fname = File.Combine(dir,fname)
Dim i, j, w, h, colour, itype As Int
itype = 1
w = im.Width
h = im.Height
Dim pos As Int = fname.IndexOf(".")
Dim format As String = fname.ToLowerCase.SubString2(pos+1,fname.Length)
' Log(""""&format&"""")
If format = "jpg" Then
itype = 1
else If format = "bmp" Then
itype = 2
else if format = "gif" Then
itype = 3
else if format = "png" Then
itype = 4
Else
ToastMsg.Show("Unsupport file type "&format,True)
Return
End If
'create a new bufferedimage (supported in java.awt.image.BufferedImage)
bufImg = nativeMe.RunMethodJO("NewBufImage",Array(w, h, itype))
For j = 0 To h-1 ' copy coloured pixels from fximage to bufferedimage
For i = 0 To w-1
colour = im.GetPixel(i,j)
bufImg.RunMethod("setRGB",Array(i,j,colour))
Next
Next
' save buffered image to file
nativeMe.RunMethod("SaveImage",Array(bufImg,format,fname))
'The Java version for Raspberry Pi as recommended in B4J forum does not support
'javafx.embed.swing.SwingFXUtil so the above solution was devised.
'On other systems use the line below without copying pixels.
'nativeMe.RunMethod("SavefxImage",Array(im,format,fname))
End Sub
#if Java
import javafx.scene.image.Image;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javafx.embed.swing.SwingFXUtils;
public void SaveImage(java.awt.image.BufferedImage image,String format, String fname)throws IOException {
ImageIO.write(image, format, new File(fname));
}
public void SavefxImage(javafx.scene.image.Image fxImage,String format, String fname)throws IOException {
java.awt.image.BufferedImage image = SwingFXUtils.fromFXImage(fxImage, null);
ImageIO.write(image, format, new File(fname));
}
public BufferedImage NewBufImage(Integer wide,Integer high, Integer itype)
{
java.awt.image.BufferedImage buffImg;
if (itype > 2) {
buffImg = new BufferedImage(wide,high,BufferedImage.TYPE_INT_ARGB);
}
else
{
buffImg = new BufferedImage(wide,high,BufferedImage.TYPE_INT_RGB);
}
return buffImg;
}
#end if