B4A Library [Class] CameraEx - Extends the camera library functionality

Status
Not open for further replies.
Example based on B4XPages: https://www.b4x.com/android/forum/threads/b4x-b4xpages-barcode-reader.120417/#content

CameraEx class wraps the Camera object and using reflection and other code it extends its functionality.

CameraEx class requires Android 2.3+ and depends on Camera library v2.20+
CameraEx features:
  • Easily open the back or front camera
  • Preview images and saved images orientation will match the device orientation (all orientations are supported)
  • Gives access to Camera.Parameters native class (flashmode, picture size, effects and other settings)
  • Includes methods to convert preview images to JPEG and to save the taken pictures.
  • It should be simple to add more methods to this class

SS-2012-11-28_12.18.31.png


See this page for the constant values and other possible methods:
https://developer.android.com/reference/android/hardware/Camera.Parameters.html

Note that you should call CommitParameters after changing one or more parameters.

CameraExClass module is included in the attached example.

V1.30 is attached - Includes various new methods.

v1.20 - Includes all the various posts in this thread as well as AutoFocusAndTakePicture method which first calls AutoFocus and then takes a picture (if AutoFocus was successful).

Edit (06/2018): A new version was uploaded with targetSdkVersion set to 26.
 

Attachments

  • CameraEx.zip
    13.2 KB · Views: 4,274
Last edited:

Shadow&Max

Active Member
Licensed User
Longtime User
I'm not... I have a blank panel declared in both the designer and the module specifically for this photo thing. Originally I called it pnlPhoto, but before posting last night, as a last ditch attempt, I renamed it Panel1 to see if that would have any effect with the class... it didn't.

There IS a button in the panel... I'll remove that and see what happens...
 

Shadow&Max

Active Member
Licensed User
Longtime User
Thanks Erel... That was it. I still don't have it working, but the error condition is gone and it's a matter of tinkering now. I had put a button into that panel, rather than above it.

All fixed... thanks...
 

Shadow&Max

Active Member
Licensed User
Longtime User
Was having an issue... have erased it all because I think I figured out the problem... Still isn't working but it's definitely firing the Ready sub, finally... unfortunately, it's always firing as not ready!
 
Last edited:

Shadow&Max

Active Member
Licensed User
Longtime User
Erel, I'm stuck...

I can't get the camera enabled... I know the panel is there because I changed the backcolor to yellow to be sure...

B4X:
Sub btnShoot_Click
    camEx.TakePicture
End Sub

Private Sub InitializeCamera
    camEx.Initialize(pnlPhoto, frontCamera, Me, "Camera1")
    frontCamera = camEx.Front
End Sub

Sub Camera1_Ready (Success As Boolean)
    If Success Then
        camEx.SetJpegQuality(90)
        camEx.CommitParameters
        camEx.StartPreview
        Log(camEx.GetPreviewSize)
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub

Sub DelPic_Click
'    'Msgbox("Temporarily Camera!","Clicked!") This code doesn't belong here but is temporary until another control is added
    pnlPhoto.Visible = True
    pnlPhoto.BringToFront
    btnShoot.Visible = True
    btnShoot.BringToFront
    btnShoot.Enabled = True
    DoEvents
    InitializeCamera
End Sub

It initializes but always returns false. In your example, it works on both the tablet as well as my phone. Here, it's starting in DelPic_Click, the panel becomes visible and is indeed yellow.

It goes through the code above, and always fails "Cannot open camera" and then at the end sub, the panel simply goes away (I've stepped through the code...) When I cancel the window, the panel flashes, then is the last thing to show before the main screen comes back. I think it's failing trying to assign the camera to the panel, but I get no error messages at all, other that the "Cannot Open..." and then it just disappears.

I even tried renaming pnlPhoto as Panel1 in the designer and code, but does the same thing. Any ideas?
 

Shadow&Max

Active Member
Licensed User
Longtime User
I got it sorted out Erel... thanks...
 

Shadow&Max

Active Member
Licensed User
Longtime User
Hi Erel:

I have an issue with the library and the effects. I thought it might have been my code because I was sending the app to my phone, but it also happens in the demo/example which I also sent to the phone.

My phone is a Moto-X, with Kitkat.

When I press the "effects" button, it scrolls through all until it gets to posterize. The next press basically locks up the camera, it no longer can set the parameters, and won't cycle around again. After it gets to the end of the list, it hangs...

FYI, it cycles fine for the flash modes, but hangs on the effects.
 
Last edited:

Shadow&Max

Active Member
Licensed User
Longtime User
Fails in release mode on both the example and my own code. The problem is that it goes through them all, then after posterize, it's locked... Up to that point, it list the modes properly... mono, negative, solarize, sepia, posterize and then the next click, it locks... doing it now with the release example. Then, after a while, it pops up the following message:

An error has occurred in sub: java.lang.RuntimeException: startPreview failed. Continue Y/N

If I click yes, it hangs for a while and displays the same message... No takes me out of the example completely. Up to that point, and if I don't use the effects, the camera's working fine... And if I cycle through the effects, they show show the physical effects properly on the screen, until after that posterize effect, when it freezes for a time before the message pops up.

Is there a way I can force it to jump at the end of the cycle and skip if it fails?
 
Last edited:

Shadow&Max

Active Member
Licensed User
Longtime User
Hi Erel:

The Effects thing does NOT generate an error. But the next move either kills the app or the camera... sometimes it recovers by hitting the back key.

Once I pass the last effect, it lags for a while, then posts the Toast Message Cannot Set Parameters, the preview freezes. In the case below, after a few seconds I clicked the Shoot (take picture) button and generated the message below. If I don't pass that last effect, the camera works fine and my picture is taken and returned to the previous screen as I'd like. The camera module in my project is photoshoot1.

** Activity (addedit) Resume **
** Activity (addedit) Pause, UserClosed = false **
** Activity (photoshoot1) Create, isFirst = true **
** Activity (photoshoot1) Resume **
[Height=480, IsInitialized=false, Width=640
]
(RuntimeException) java.lang.RuntimeException: setParameters failed
** Activity (photoshoot1) Pause, UserClosed = false **
** Activity (photoshoot1) Create, isFirst = false **
** Activity (photoshoot1) Resume **
Error occurred on line: 108 (cameraexclass)
java.lang.RuntimeException: startPreview failed
at android.hardware.Camera.startPreview(Native Method)
at anywheresoftware.b4a.objects.CameraW.StartPreview(CameraW.java:177)
at com.twodogapps.pts.cameraexclass._startpreview(cameraexclass.java:140)
at com.twodogapps.pts.photoshoot1._camera1_ready(photoshoot1.java:533)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:858)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:815)
at com.twodogapps.pts.cameraexclass._camera_ready(cameraexclass.java:444)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:305)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at anywheresoftware.b4a.objects.CameraW$2$1.run(CameraW.java:139)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5083)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
** Activity (photoshoot1) Pause, UserClosed = false **
 

Shadow&Max

Active Member
Licensed User
Longtime User
Yes, I can only see it happening on my phone... I have a crappy tablet and that cycles through fine.

The other bizarre thing is that the effects are NOT offered on any of the camera's controls...
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
What resolution(s) did you set for the glass and what aspect ratio ?
 

Shadow&Max

Active Member
Licensed User
Longtime User
Alberto... because it's in landscape, the picture size button is not showing. Go into the Designer and move that button up.

Erel's example is for Portrait mode.

You can just see the top of it at the bottom of your screen...
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
I try this and I put a log in get preview sizes, look the results:

Width x Height = 2592 x 1944
Width x Height = 2560 x 1888
Width x Height = 2528 x 1856
Width x Height = 2592 x 1728
Width x Height = 2592 x 1458
Width x Height = 2560 x 1888
Width x Height = 2400 x 1350
Width x Height = 2304 x 1296
Width x Height = 2240 x 1344
Width x Height = 2160 x 1440
Width x Height = 2112 x 1728
Width x Height = 2112 x 1188
Width x Height = 2048 x 1152
Width x Height = 2048 x 1536
Width x Height = 2016 x 1512
Width x Height = 2016 x 1134
Width x Height = 2000 x 1600
Width x Height = 1920 x 1080
Width x Height = 1600 x 1200
Width x Height = 1600 x 900
Width x Height = 1536 x 864
Width x Height = 1408 x 792
Width x Height = 1344 x 756
Width x Height = 1296 x 972
Width x Height = 1280 x 1024
Width x Height = 1280 x 720
Width x Height = 1152 x 864
Width x Height = 1280 x 960
Width x Height = 1024 x 768
Width x Height = 1024 x 576
Width x Height = 640 x 480
Width x Height = 320 x 240
[Height=360, IsInitialized=false, Width=640

I try settings like this after camera ready, but nothing.....

camEx.SetPictureSize(320,240)
 
Status
Not open for further replies.
Top