B4J Question WebP images and B4J, is it possible to convert jpg to webp ?

Magma

Well-Known Member
Licensed User
Hi there...

last days i was testing webp images and i have to say they are very good ! :)

B4J has a library here... that can help load them as a simple image...

But is it possible to convert bytes or image of jpeg for example ---> to bytes of webp and how (with source code at b4j) ?

ps: Some will tell - why you didn't just change the quality of jpeg...
image.WriteToStream(out, quality, "JPEG")
but what i want is something like this:
image.WriteToStream(out, quality, "WEBP")
but there is not an option for webp :-(
 
Last edited:

MarcoRome

Expert
Licensed User
Look this:
 
Upvote 0

Magma

Well-Known Member
Licensed User
it doesnt seems to work... :-(
 
Upvote 0

Magma

Well-Known Member
Licensed User
actually getting this when i try to convert a jpg file to webp:

ΞΞ±Ο 23, 2021 2:21:42 Ξ.Ξ. org.scijava.nativelib.BaseJniExtractor extractJni
INFO: Couldn't find resource META-INF/lib/windows_64/ webp-imageio.dll
ΞΞ±Ο 23, 2021 2:21:42 Ξ.Ξ. org.scijava.nativelib.NativeLibraryUtil loadNativeLibrary
WARNING: IOException creating DefaultJniExtractor
java.io.IOException: Couldn't find resource META-INF/lib/windows_64/ webp-imageio.dll
at org.scijava.nativelib.BaseJniExtractor.extractJni(BaseJniExtractor.java:158)
at org.scijava.nativelib.NativeLibraryUtil.loadNativeLibrary(NativeLibraryUtil.java:287)
at com.luciad.imageio.webp.WebP.loadNativeLibrary(WebP.java:29)
at com.luciad.imageio.webp.WebP.<clinit>(WebP.java:34)
at com.luciad.imageio.webp.WebPEncoderOptions.<clinit>(WebPEncoderOptions.java:20)
at com.luciad.imageio.webp.WebPWriteParam.<init>(WebPWriteParam.java:30)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:91)
at b4j.example.webp._converttowebp(webp.java:56)
at b4j.example.main._button2_mouseclicked(main.java:155)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:110)
at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:1)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3564)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Scene.java:3492)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3860)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
webp._converttowebp (java line: 56)
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:91)
at b4j.example.webp._converttowebp(webp.java:56)
at b4j.example.main._button2_mouseclicked(main.java:155)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:110)
at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:1)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3564)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Scene.java:3492)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3860)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.UnsatisfiedLinkError: com.luciad.imageio.webp.WebPEncoderOptions.createConfig()J
at com.luciad.imageio.webp.WebPEncoderOptions.createConfig(Native Method)
at com.luciad.imageio.webp.WebPEncoderOptions.<init>(WebPEncoderOptions.java:26)
at com.luciad.imageio.webp.WebPWriteParam.<init>(WebPWriteParam.java:30)
... 45 more
 
Upvote 0

Magma

Well-Known Member
Licensed User
Upvote 0

Magma

Well-Known Member
Licensed User
If someone get into this thread will read my monologue :)

Searching all over ther internet..


Found this: https://github.com/zakgof/webp4j and hey... remember that Erel wrapped this library...

scrolling down the page and i ve seen what i ve been looking...

B4X:
// Encode
BufferedImage bi = ...
float quality = 80f;
byte[] bytes = Webp4j.encode(bi, quality);

// Decode
BufferedImage restored = Webp4j.decode(bytes);

but hey in B4J Library WebP... there is only Load from file... not encode/decode :-( . ... is there a way with JavaObject or inline java someway ?
 
Upvote 0

moster67

Expert
Licensed User
Never tried it but you can probably use ffmpeg (calling it from jShell) using a command like this:
B4X:
ffmpeg -i file.webp out.png
or
ffmpeg -i file.webp out.jpg
 
Upvote 0

Magma

Well-Known Member
Licensed User
Never tried it but you can probably use ffmpeg (calling it from jShell) using a command like this:
B4X:
ffmpeg -i file.webp out.png
or
ffmpeg -i file.webp out.jpg
Need the reverse

image.WriteToStream(out, quality, "WEBP")

Will be better to have them in bytes without messing files... And without the need of ffmpeg

I will check the webp B4xlib and try to extend it with encode as Erel said to me .. hope i ll have time on weekend
 
Upvote 0

Quandalle

Member
Licensed User
Here is an example of conversion using cwebp.exe the webp image compressor developed by Google

B4X:
Public Sub jpeg2webp(dir As String ,fn As String)
    Dim shl As Shell
    ' call convert doing resize 
    Dim fullpath As String = File.Combine(dir,fn)
    shl.Initialize("shl", File.DirApp &"\cwebp.exe", Array As String("-resize","0","750",fullpath, "-o",fullpath.SubString2(0,fullpath.Length-4) & "webp"))
    shl.WorkingDirectory = File.dirapp
    shl.Run(-1)
    wait for shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Not(Success) Then
        Log(StdErr )
    End If
End Sub
 
Upvote 0
Top