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: 3,992
Last edited:

wimpie3

Well-Known Member
Licensed User
Longtime User
I get this:
1179648
768,1024
0
768,1024
followed by:
Fatal signal 6 (SIGABRT) at 0x00006f90 (code=-6), thread 28560 (test.android)

Yes, i'm working with the same values. Perhaps it is a good idea to recompile the NV21toRGB library and activate some debugging stuff?
 

JordiCP

Expert
Licensed User
Longtime User
Parameters seem correct

Do you make a copy of the preview buffer each time? If so, do you allocate memory each time for it? It should be avoided. That's also the why of working always with the same mutable bitmap

Also, have you tried with a lower resolution?
 

wimpie3

Well-Known Member
Licensed User
Longtime User
I'm making a copy every time, that's correct. But trust me, even if I put the conversion to RGB right inside the preview event, and do not make a copy, the app crashes as well. And yes, also with a smaller preview of 800x600. I've tested this on my Nexus 5 (=sigfault). On my old HTC there is no sigfault but the app just hangs.
 

JordiCP

Expert
Licensed User
Longtime User
I don't know much about this JVM error signal.:(

is it possible to send a subset of the project where it happens so I can try it?
also, unfiltered logs should give more information


@wimpie3: I think it is better to continue this discussion in the NV21Util thread, since it is more related to that than to the Camera library itself
 

hanyelmehy

Active Member
Licensed User
Longtime User
Hi,
any one found this problem
some time when take a picture ,project crash ,and crash system camera it self
 

wimpie3

Well-Known Member
Licensed User
Longtime User
The problem is as follows: taking a picture from the camera works fine on my HTC with Android 2.3.3 (100% of the time) but fails 90% of the time (NOT always!) on my Nexus 5 with Android 4.4.2.

I have modified the TakePicture sub in the camex class to display a line in the log:
B4X:
Public Sub TakePicture
Log("start takepicture class")
cam.TakePicture
Log("stop takepicture class")
End Sub

Then I do this in my main sub:
B4X:
camEx.SetPreviewSize(1024,768)
camEx.SetPictureSize(1024,768)
camEx.JpegQuality=90
camEx.SetContinuousAutoFocus
camEx.CommitParameters
camEx.StartPreview

And of course:
B4X:
camEx.TakePicture

This is the error log I get. I see references to Out of memory, which is IMHO not possible.
B4X:
start takepicture class
camera_metadata_t* qcamera::QCamera3HardwareInterface::translateCapabilityToMetadata(int): Setting focus mode to off
stop takepicture class
GC_CONCURRENT freed 1K, 1% free 38711K/38936K, paused 2ms+3ms, total 21ms
[SurfaceView] dequeueBuffer: BufferQueue has been abandoned!
Failed to call ioctl MSMFB_OVERLAY_SET err=Out of memory
MdpCtrl failed to setOverlay, restoring last known good ov info
== Bad OVInfo is:  mdp_overlay z=1 fg=0 alpha=255 mask=-1 flags=0x64000 id=-1
src msmfb_img w=1152 h=96 format=13 MDP_RGBA_8888
src_rect mdp_rect x=0 y=0 w=1080 h=75
dst_rect mdp_rect x=0 y=0 w=1080 h=75
getBufferLocked: Stream 0: Can't dequeue next output buffer: No such device (-19)
RequestThread: Can't get output buffer, skipping request: No such device (-19)
== Last good known OVInfo is:  mdp_overlay z=0 fg=0 alpha=0 mask=0 flags=0x0 id=-1
src msmfb_img w=0 h=0 format=0 MDP_RGB_565
src_rect mdp_rect x=0 y=0 w=0 h=0
dst_rect mdp_rect x=0 y=0 w=0 h=0
MdpCtrl Lkgo ov has id -1, will not restore
Ctrl commit failed set overlay
configureLowRes: commit failed for low res panel
Process com.facebook.katana:dash (pid 15669) has died.
Trimming memory (onTrimMemory 40)
Low Memory: Freed 0 resources from compression queue, 0 from write queue, 0 from the pinned list, 0 from the cache.
doTerminate failed: EGL count is 2 but managed count is 1
[SurfaceView] queueBuffer: BufferQueue has been abandoned!
queueBuffer: error queuing buffer to SurfaceTexture, -19
returnBufferCheckedLocked: Stream 0: Error queueing buffer to native window: No such device (-19)
Can't return buffer 1 for frame 14 to its stream:  No such device (-19)
[SurfaceView] queueBuffer: BufferQueue has been abandoned!
queueBuffer: error queuing buffer to SurfaceTexture, -19
returnBufferCheckedLocked: Stream 0: Error queueing buffer to native window: No such device (-19)
Can't return buffer 1 for frame 15 to its stream:  No such device (-19)
[SurfaceView] queueBuffer: BufferQueue has been abandoned!
queueBuffer: error queuing buffer to SurfaceTexture, -19
returnBufferCheckedLocked: Stream 0: Error queueing buffer to native window: No such device (-19)
Can't return buffer 1 for frame 16 to its stream:  No such device (-19)
mm_stream_read_msm_frame: Stoping poll on stream 0xb7d124a4 type :1
mm_stream_read_msm_frame: Stopped poll on stream 0xb7d124a4 type :1
mm_stream_read_msm_frame: Stoping poll on stream 0xb7d132f4 type :5
mm_stream_read_msm_frame: Stopped poll on stream 0xb7d132f4 type :5
[SurfaceView] queueBuffer: BufferQueue has been abandoned!
queueBuffer: error queuing buffer to SurfaceTexture, -19
returnBufferCheckedLocked: Stream 0: Error queueing buffer to native window: No such device (-19)
Can't return buffer 1 for frame 17 to its stream:  No such device (-19)
GC_CONCURRENT freed 136K, 4% free 18104K/18732K, paused 2ms+1ms, total 17ms
WAIT_FOR_CONCURRENT_GC blocked 10ms
GC_CONCURRENT freed 265K, 3% free 18249K/18732K, paused 1ms+1ms, total 21ms
[1] 5.onFinished: Installation state replication succeeded.
Timed out waiting for capture to complete
 

wimpie3

Well-Known Member
Licensed User
Longtime User
Added largeHeap. No change. And yes, the picture size is supported, if you restart the app often enough there is a chance of 10% the image DOES get captured. It has nothing to do with lack of memory, my HTC with Android 2.3 has less memory and my app works 100% of the time over there.
 

wimpie3

Well-Known Member
Licensed User
Longtime User
Yes, always the same crash... before you ask me to see the source code, that is simply not possible because there is too much code. If I use the demo program that comes with camex, everything works just fine. I will try to isolate the problem but that will cost a lot of time.
 

wimpie3

Well-Known Member
Licensed User
Longtime User
I have managed to isolate the code. If you have a Nexus 5, the attached program does NOT take a picture 90% of the time. You have to launch it several times to get the pictureTaken sub to fire.
 

Attachments

  • camerabug.zip
    10.1 KB · Views: 196

wimpie3

Well-Known Member
Licensed User
Longtime User
That did the trick. Unfortunately, it still isn't working in my "real" program, where taking a picture is done with a press on a button (so there is already a delay between StartPreview and TakePicture). I continue to investigate.
 

wimpie3

Well-Known Member
Licensed User
Longtime User
Mystery solved. Between TakePicture and the callback fired when the picture is taken, I removed the original panel containing the camera image. This is a big no no, apparently the panel must be available until the end of this procedure. Timing issues made it difficult to find this problem, sometimes it worked, sometimes it didn't.
 

wimpie3

Well-Known Member
Licensed User
Longtime User
I did discover something new though. The live image from the front camera is shown fine on the screen, but as soon as I take a picture, the result is mirrored. This only happens with the front cam.
 

LorenzoTRANSFEREX

Member
Licensed User
Longtime User
Create a global mutable bitmap and canvas. You can then convert the Data array to an image and draw it with the canvas on the bitmap. You will also be able to draw text with this canvas over the bitmap.

Hi Erel,

Could you give any code example for stamp any test in a picture or send me to the right post?
I am trying to do by myself using ACL library, but I am not able to do the right sentence:

B4X:
Sub camara_PictureTaken(Data() As Byte)
   
  Camara.StartPreview

  Dim salida As OutputStream, picture As Canvas, Bmp As Bitmap, Recta As Rect

  salida = File.OpenOutput(File.DirRootExternal,"foto.jpg",False)
  salida.WriteBytes(Data,0,Data.Length)
  salida.Close
 
  Bmp.Initialize (File.DirRootExternal,"foto.jpg")
  Recta.Initialize(10dip,10dip,110dip,110dip) 'this is only for test purposes
  picture.DrawBitmap(Bmp, Null, Recta)
  picture.DrawText("Test text stamp",200dip,200dip,Typeface.DEFAULT_BOLD,30,Colors.White,"LEFT")  
  picture.Bitmap.WriteToStream(salida,100,"JPEG")

...

Thanks a lot
 

bsnqt

Active Member
Licensed User
Longtime User
@lorenzo:

Try this:

B4X:
    Private Canvas1 As Canvas
    Dim rect1 As Rect
    rect1.Initialize(0,0,w,h)
    Dim image2 As Bitmap
    image2.InitializeMutable(w,h)
    Canvas1.Initialize2(image2)
    Canvas1.DrawBitmap(LoadBitmap(dir,filename),Null,rect1)
    Canvas1.DrawText(DateTime.Date(DateTime.Now)& ", " & DateTime.Time(DateTime.Now), w*2/3,h*34/35, Typeface.DEFAULT, w/100, Colors.Red, "LEFT")
    Private out As OutputStream
    out = File.OpenOutput(dir,filename,False)
    Canvas1.Bitmap.WriteToStream(out,100,"JPEG")
    out.Flush
    out.Close
 
Status
Not open for further replies.
Top