I need to be able to create a JPEG output stream in B4J so I can save images arriving via FTP in AWS S3 buckets as JPEGs (PNG doesn't cut it).
In B4A I was doing this without problems with code of form:
....WriteToStream(wrk_out, jpg_quality, "JPEG")
B4J apparently doesn't support JPEG (although B4A does - another of life's little mysteries).
I have been playing around with code I found here:
http://www.java2s.com/Code/Java/2D-...fileTheJPEGqualitycanbespecifiedinpercent.htm
So far I have:
Which dies with:
I can find no way around this and I really need to get it resolved - any and all help appreciated...
In B4A I was doing this without problems with code of form:
....WriteToStream(wrk_out, jpg_quality, "JPEG")
B4J apparently doesn't support JPEG (although B4A does - another of life's little mysteries).
I have been playing around with code I found here:
http://www.java2s.com/Code/Java/2D-...fileTheJPEGqualitycanbespecifiedinpercent.htm
So far I have:
B4X:
Private Sub WriteToStreamJPEG(In_image As Image, JPEG_quality As Int) As OutputStream
Private wrk_out As OutputStream
wrk_out.InitializeToBytesArray(0)
nativeMe.RunMethod("saveImageAsJPEG", Array(In_image, wrk_out, JPEG_quality))
' nativeMe.RunMethod("saveImageAsJPEG", Array(Array As Image (In_image), Array As OutputStream (wrk_out), Array As Int (JPEG_quality)))
Return wrk_out
End Sub
#If Java
//See http://www.java2s.com/Code/Java/2D-Graphics-GUI/WritesanimagetoanoutputstreamasaJPEGfileTheJPEGqualitycanbespecifiedinpercent.htm
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Locale;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
/**
* Writes an image to an output stream as a JPEG file. The JPEG quality can
* be specified in percent.
*
* @param image
* image to be written
* @param stream
* target stream
* @param qualityPercent
* JPEG quality in percent
*
* @throws IOException
* if an I/O error occured
* @throws IllegalArgumentException
* if qualityPercent not between 0 and 100
*/
public static void saveImageAsJPEG(BufferedImage image,
OutputStream stream, int qualityPercent) throws IOException {
if ((qualityPercent < 0) || (qualityPercent > 100)) {
throw new IllegalArgumentException("Quality out of bounds!");
}
float quality = qualityPercent / 100f;
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
writer = (ImageWriter) iter.next();
}
ImageOutputStream ios = ImageIO.createImageOutputStream(stream);
writer.setOutput(ios);
ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault());
iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwparam.setCompressionQuality(quality);
writer.write(null, new IIOImage(image, null, null), iwparam);
ios.flush();
writer.dispose();
ios.close();
}
#End If
Extensive googling would suggest my problem is trying to pass a B4J Image to a java BufferedImage.Waiting for debugger to connect...
Program started.
Error occurred on line: 1381 (Main)
java.lang.RuntimeException: Method: saveImageAsJPEG not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:129)
at b4j.example.main._writetostreamjpeg(main.java:2695)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:61)
at b4j.example.main$ResumableSub_Photo_uploader.resume(main.java:520)
at b4j.example.main._photo_uploader(main.java:453)
at b4j.example.main._event_obj_check_backlog_timer_tick(main.java:432)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.objects.Timer$TickTack$1.run(Timer.java:118)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
I can find no way around this and I really need to get it resolved - any and all help appreciated...