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

NeoTechni

Well-Known Member
Licensed User
Longtime User
I have the preview size set to like 128x128 but I get a 5 megapixel one on my n5
 

Steini1980

Active Member
Licensed User
Longtime User
I have extended the cameraEx.bas Class, you could extend the Class too, if you copy new function at the end of class:

B4X:
Public Sub getMinExposureCompensation As Int
    r.target = parameters
    Return r.RunMethod("getMinExposureCompensation")
End Sub

Public Sub getMaxExposureCompensation As Int
    r.target = parameters
    Return r.RunMethod("getMaxExposureCompensation")
End Sub

Public Sub getExposureCompensation As Int
    r.target = parameters
    Return r.RunMethod("getExposureCompensation")
End Sub

Public Sub setExposureCompensation(ev As Int)
    r.target = parameters
    r.RunMethod2("setExposureCompensation",ev,"java.lang.int")
End Sub

Public Sub isAutoExposureLockSupported As Boolean
    r.target = parameters
    Return r.RunMethod("isAutoExposureLockSupported")
End Sub

Public Sub getAutoExposureLock As Boolean
    r.target = parameters
    Return r.RunMethod("getAutoExposureLock")   
End Sub

Public Sub setAutoExposureLock(toggle As Boolean)
    r.target = parameters
    r.RunMethod2("setAutoExposureLock",toggle,"java.lang.boolean")  
End Sub
 

Stefan Doerr

New Member
Licensed User
Longtime User
Hi,

I hope I just didn't find the problem/solution in all the pages here, but probably someone knows the issue:

I got a Image by picturetaken or the preview image and try to send this via a network stream to a Windows .net-Client. The Client receives the Image but I can't show it. If I store the byte-stream to the disk the Image is corrupted. I still see the Header (with EXIF-text) but it looks no tool can open this Image.

Anyone knows the issue? Is it related to the Picture-data or could it be a problem transferring the Image from Android to Windows via Network?

Thanks!
 

Stefan Doerr

New Member
Licensed User
Longtime User
EDIT: Works now. Code changed to have an example for others. The Code to transfer the Image-size is surely not optimized but it works. Thank you

I tried it with PictureTaken first with the same result. I use this in B4A:

B4X:
Sub Camera1_preview (data() AsByte)
  If connected = True Then
     Try
         Dim preview() AsByte
         preview = camEx.PreviewImageToJpeg(data,100)
         Dim orgsize(1) AsInt
         orgsize(0) = preview.Length
         Dim bc As ByteConverter
         Dim sizes() AsByte
         sizes = bc.IntsToBytes(orgsize)
         OutputStream1.WriteBytes(sizes,0,4)
         OutputStream1.WriteBytes(preview,0,preview.Length)
      Catch
         connected = False
      End Try
  End If
End Sub

in VB.NET the following Code receives the Image:

B4X:
  ' client =NewTcpClient("myclientip_or_name", 2222)
  ' s = client.GetStream
  ' b =New IO.BinaryReader(s)
  Dim l As Integer = IPAddress.NetworkToHostOrder(b.ReadInt32)
  Dim myReadBuffer(l) As Byte
  Dim numberOfBytesRead As Integer = 0
  numberOfBytesRead = s.Read(myReadBuffer, 0, myReadBuffer.Length)
  Try
  PictureBox1.Image = Image.FromStream(New IO.MemoryStream(myReadBuffer))
  Catch
  End Try
 
Last edited:

bjf

Member
Licensed User
Longtime User
I'm having trouble with this on Samsung S5 Active, running 5.0 lollipop.
It seems the PictureTaken sub is never called.
Flash fires and everything but the picture is not saved and panels that i show in PictureTaken are never shown.

It works flawless on S4 active with 4.4.2 so what might be the problem?
I have compiled it in B4A 4.00
 

bjf

Member
Licensed User
Longtime User
I have now tested with the attached example from first post and that works, so it seems it's not liking something in my implementation.

In my code i do have a few lines of code after camex.TakePicture, but could that really cause the problem?

B4X:
camEx.SetPictureSize(3264,1836)
camEx.SetJpegQuality(100)
camEx.CommitParameters
camEx.TakePicture   
        
CamChecked.Checked=True
PicPanel.RemoveView
CamPanel.RemoveView
FlashCheck.Checked=False

I realized this just now and right now i don't have access to the phone in question but i will try disabling the rows later
 

bjf

Member
Licensed User
Longtime User
Picture size is supported and it managed to commit it.
Problem was indeed the lines of code after TakePicture.
Moved them to PictureTaken and problem solved
 

scsjc

Well-Known Member
Licensed User
Longtime User
Hello,

Is possible have a 2 images from camera ?
1 panel - without efects
1 panel - with efects

if is not possible, can do any RSPROCESSING image in realtime ??? and have a result on a diferent panel ?

thanks !!!!
 

MaFu

Well-Known Member
Licensed User
Longtime User
Hello,

Is possible have a 2 images from camera ?
1 panel - without efects
1 panel - with efects

if is not possible, can do any RSPROCESSING image in realtime ??? and have a result on a diferent panel ?

thanks !!!!
Like this?
 

MaFu

Well-Known Member
Licensed User
Longtime User
I think yes, but i don't load on my SamsungGalaxy 5, lollipop , crash !!!
This apk is compiled with an older version of B4A. Maybe it's not compatible with Lollipop.
I will create a new version soon.
 

scsjc

Well-Known Member
Licensed User
Longtime User
I uploaded a new version of my camera test app. I hope it runs now on your Galaxy. Runs fine on my Xperia Z3 with 30 fps.
Hello, yes now work, but is very slow on my device.
i only make a blur efect on border from main image (i want make a efect like real mirror...)

thanks
 

MaFu

Well-Known Member
Licensed User
Longtime User
Hello, yes now work, but is very slow on my device.
i only make a blur efect on border from main image (i want make a efect like real mirror...)

thanks
Strange. Which camera resolution did you set?
I have 30 fps on 640x480 and 17/18 fps on 1280x720.
 
Status
Not open for further replies.
Top