B4J Question How to take picture from camera?

bjfhs

Active Member
Licensed User
Longtime User
I want to preview and take picture from a usb camera,how can i do?
When I use the code(https://www.b4x.com/android/forum/threads/class-webcam-capture.53211/),I get error
B4X:
Waiting for debugger to connect...
Program started.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Default cam: (JavaObject) Not initialized
Error occurred on line: 26 (Webcam)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:32)
    at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:259)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
    at b4j.example.webcam._setdimension(webcam.java:99)
    at b4j.example.main._appstart(main.java:83)
    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:627)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:236)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    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:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at b4j.example.main.start(main.java:38)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
 

bjfhs

Active Member
Licensed User
Longtime User
When I use java inline(javacv),I can't open a new window for the camera.Can you give me any advice else?
B4X:
  NativeMe.RunMethod("showVideo",Null)
B4X:
public static void showVideo() {
   OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); 
    try{
    grabber.start();  
    CanvasFrame canvas = new CanvasFrame("camera");
    canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas.setAlwaysOnTop(true);
   
    while(true)
    {
        if(!canvas.isDisplayable())
        {
            grabber.stop();
            System.exit(2);
        }
        canvas.showImage(grabber.grab());
       
        Thread.sleep(500);
    }
    }
    catch (Exception e)
    {
    }
 }
 
Upvote 0
Top