B4A Library Advanced Camera Library V1.00

UPDATE! - Zoom, Current & Supported - Advanced Camera Library V2.00

Hello everyone!

B4X:
Sub Globals
Dim camera1 As AdvancedCamera ' New lib
End Sub


I'm glad to announce my fourth library! :D
This library is a modification of the existing camera library with more options.
So the standard procedures of the camera still apply.
Basic4android - Camera

You can find the Advanced Camera Library in the attachements.
To use: Extract and Copy the Advanced Camera.xml and Advanced Camera.jar to the library folder in "Anywhere Software\Basic4android\Libraries".

.:!BIG UPDATE!:.
Advanced Camera Library has been updated to version 2.0 with tons of new features!
See post #19
http://www.b4x.com/forum/52479-post19.html


.:Index:.


[1] Flashlight
[2] Camera's
[3] Orientation
[4] Pictures
[5] Antibanding
[6] Colour Effects
[7] Focus Mode
[8] Scene Mode
[9] White Balance


So, let's start

1) Flashlight

a.
"FlashAuto": Enables the flashlight to turn on automatically.
B4X:
Camera1.FlashAuto()

b.
"FlashOn": Turns the flashlight on.
B4X:
Camera1.FlashOn()

c.
"FlashOff": Turns the flashlight off.
B4X:
Camera1.FlashOff()

2) Camera's

a.
"CameraBack": Sets the back camera as primary camera.
B4X:
Camera1.CameraBack()

b.
"CameraFront": Sets the front camera as primary camera.
B4X:
Camera1.CameraPortrait()

3) Orientation

a.
"OriLandscape": Sets the orientation to landscape mode.
B4X:
Camera1.OriLandscape()

b.
"OriPortrait": Sets the orientation to portrait mode.
B4X:
Camera1.OriPortrait()

4) Pictures

a.
"PictureSize": Sets the size of the picture in pixels.
The parameters must be modified before Camera1.StartPreview.
B4X:
Camera1.PictureSize(480,320)

b.
"Quality": Sets the quality of the JPEG picture. Range between 0 - 100, with 100 being the best.
The parameters must be modified before Camera1.StartPreview.
B4X:
Camera1.Quality = 100

5) Antibanding

"setAntibanding": Sets the Antibanding. This is used to remove noises like when you take a picture of a monitor/television screen.
  • ANTIBANDING_50HZ: Sets the antibanding to 50Hz.
  • ANTIBANDING_60HZ: Sets the antibanding to 60Hz.
  • ANTIBANDING_AUTO: Sets the antibanding to automatic mode.
  • ANTIBANDING_OFF: Sets the antibanding off.
B4X:
Camera1.setAntibanding = "ANTIBANDING_AUTO"

6) Colour Effects

a.
"setEffect": Sets the color effect of your picture.
  • AQUA
  • BLACKBOARD
  • MONO
  • NEGATIVE
  • NONE
  • POSTERIZE
  • SEPIA
  • SOLARIZE
  • WHITEBOARD
B4X:
Camera1.setEffect = "SEPIA"

7) Focus Mode

"setFocusMode": Sets the focus mode of your picture.
  • MACRO: Close-up.
  • INFINITY: Far distance.
  • FIXED: If the camera has auto-focus, this mode can fix the focus.
  • AUTO: Auto-focus mode.
  • EDOF: Focusing is done digitally and continuously.
B4X:
Camera1.setFocusMode = "MACRO"

8) Scene Mode

"setSceneMode": Sets the scene mode of your picture.
  • ACTION: Take photos of fast moving objects.
  • AUTO: Scene mode is off.
  • BEACH: Take pictures on the beach.
  • CANDLELIGHT: Capture scenes lit by candles.
  • FIREWORKS: Take pictures of fireworks.
  • LANDSCAPE: Take pictures on distant objects.
  • NIGHT: Take photos at night.
  • PARTY: Indoor low-light shot.
  • PORTRAIT: Take people pictures.
  • SNOW: in the snow.
  • STEADYPHOTO: Avoid blurry pictures (handshake).
  • SUNSET: Take Sunset photos.
  • THEATRE: Take photos in a theater.
B4X:
Camera1.setScene = "BEACH"

9) White Balance

"setWhiteBalance": Sets the white balance of your picture.
  • AUTO
  • CLOUDY_DAYLIGHT
  • DAYLIGHT
  • FLUORESCENT
  • INCANDESCENT
  • SHADE
  • TWILIGHT
  • WARM_FLUORESCENT
B4X:
Camera1.setWhiteBalance = "WARM_FLUORESCENT"


If there are any bugs, questions or features you want to add to the library, please post them on this forum.

Credits and feedback are really much appreciated!

Thank you very much!

XverhelstX

Full example:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Dim camera1 As AdvancedCamera
   Dim Panel1 As Panel
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
   Activity.LoadLayout("Menu")
   activity.AddMenuItem("Take Picture","mnuTakePicture")
   activity.AddMenuItem("Set Effect","mnuSetEffect")
   activity.AddMenuItem("Flash On","mnuFlashOn")
   activity.AddMenuItem("Focus Mode","mnuFocusMode")
   
End Sub

Sub Activity_Resume
   
   camera1.Initialize(panel1, "Camera1")
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   camera1.StopPreview
   camera1.Release
End Sub

Sub mnuFlashOn_Click

camera1.FlashOn()
End Sub

Sub mnuSetEffect_Click

Camera1.setEffect = "SEPIA"

End Sub

Sub Camera1_Ready (Success As Boolean)
   If success Then
      Camera1.StartPreview
      
   Else
      ToastMessageShow("Cannot open camera.", True)
   End If
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
   camera1.StartPreview
   Dim out As OutputStream
   out = File.OpenOutput(File.DirRootExternal, "Image.jpg", False)
   out.WriteBytes(data, 0, data.Length)
   out.Close
   ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal, "Image.jpg"), True)

   
End Sub

Sub mnuTakePicture_Click
   
   camera1.TakePicture
   
End Sub

Sub mnuFocusMode_Click
   
   camera1.setFocusMode = "MACRO"
   
End Sub
 

Attachments

  • ACL4.4.zip
    364.7 KB · Views: 2,075
  • ACL4.5.zip
    33.5 KB · Views: 1,489
  • ACL4.6.zip
    359.4 KB · Views: 5,006
Last edited:

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Great Library with a lot of advanced features it will take me forever to make use of all of them.

1. Sub Camera1_PreviewTaken (PictureFrame() As Byte)

and

Sub Camera1_PictureTaken2 (Data() As Byte)

do not come up in the possible events when you type Sub then hit Tab and select the AdvancedCamera library.

2. TakePicture2 describes an asBitmap that doesn't appear to exist.

3. toggleFlashlight works and appears to toggle between off and Torch on my device. The tooltip indicates a return value, but it has none.

4. I don't really see a way of getting a List/Map of supported resolutions (Width and Height...current function just gets a count of how many resolutions). I see a post here using reflection to get it which I will try, but if it doesn't work I'm not sure of how to get them.

5. Any chance on getting onPreviewFrame explained?

6. reconnect is there now too, but not explained well. Is it to be used after releasing or something instead of Initializing the object again?

7. setResolution vs. PictureSize- I haven't test it much yet, but they appear to be the same thing by name. So, is Resolution the Preview Frame Size and PictureSize is the Picture size of the photo taken? Possibly would have been better calling them PreviewSize and PictureSize if that is the case.

8. I saw a couple PHP functions that seemed odd to have in the library and as another user noted above it increases the permissions required which may not be needed. If I were to use PHP I'd use my own functions and way of accessing them. Those might be better in another library. I'd just keep simple things that this library can use better than an external like the Base64 stuff. Like I noted in #2 there is an asBitmap and it could be useful for the Picture and Preview Taken events to have the ability of the methods calling them to specify asBytes/asBase64/asBitmap.

I've also seen a lot of methods and such in B4A make use of numbers a lot...sometimes too much. A more descriptive name would be more useful like instead of:

Sub Camera1_PictureTaken2 (Data() As Byte)

we have-

Sub Camera1_PictureTakenBytes (Data() As Byte)
Sub Camera1_PictureTakenBase64 (Data() As Byte)
Sub Camera1_PictureTakenBitmap (Image As Bitmap)

I don't really see the need for a different Event for rotated pictures since they all have the same result of an image taken and rotation is just there to fix orientation problems.
 
Last edited:

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hey roger,

In the coming weeks, i'm planning to update Advanced Camera Library to ACL 2.0. A much improved Camera library with bug fixes, easier methods, more classes and more methods.

This library was one of the first i created and when i was new to Java.
So better sub names and public method names will be used. Getters, setters, etc

Tomas

Verstuurd van mijn R800i met Tapatalk
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Awesome. Can't wait to see it.

One suggestion that would really make this slick is auto handling of orientation. Use getRotation to get the orientation of the handheld whenever Starting the Preview or Taking a picture and set the needed rotation automatically.

The other thing I didn't see is more Focus options. There is Focus Modes, but all the devices I've used had the option of holding the shutter button and getting a focus before releasing and taking the shot. (I may not understand how the modes work though and maybe you set the Mode to Fixed or something for this then back to Auto on release) Some recent ones even have the ability to tap on the screen and focus on a specific point- I'm guessing if this isn't hardware specific it would be like specifying X and Y of where they tapped in % or something since the Preview window is a different size and passing that to the Camera that then focuses on that.
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
In the coming weeks, i'm planning to update Advanced Camera Library to ACL 2.0. A much improved Camera library with bug fixes, easier methods, more classes and more methods.

Is there a timeline on this? I've been trying to make use of the current library and can't seem to make it work. Releasing the Camera takes forever. Saving images also takes forever. Most of the time 0 orientation saves an image that is fine. If I have to do a 90 degree rotation save it always fails with a corrupt image that Android won't open. If I copy the file to a Windows machine it shows a half created image with the rest Black. I guess it is running out of memory or something in the rotation. If a bitmap is being created to rotate it is very likely since that would me 2 full size images in memory. Is there no way of telling the camera to rotate then pass the data? What is really odd is sometimes the app doesn't crash when saving and sometimes it does, but the saved picture in rotating is never complete.

I've also tried saving in a thread and having a progress circle show...it still seems to use all CPU and the circle doesn't animate. Sometimes the circle doesn't show either which is odd.
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
Is there a timeline on this? I've been trying to make use of the current library and can't seem to make it work. Releasing the Camera takes forever. Saving images also takes forever. Most of the time 0 orientation saves an image that is fine. If I have to do a 90 degree rotation save it always fails with a corrupt image that Android won't open. If I copy the file to a Windows machine it shows a half created image with the rest Black. I guess it is running out of memory or something in the rotation. If a bitmap is being created to rotate it is very likely since that would me 2 full size images in memory. Is there no way of telling the camera to rotate then pass the data? What is really odd is sometimes the app doesn't crash when saving and sometimes it does, but the saved picture in rotating is never complete.

I've also tried saving in a thread and having a progress circle show...it still seems to use all CPU and the circle doesn't animate. Sometimes the circle doesn't show either which is odd.

Hey Roger,

Unfortunately, I have no idea when i'll be able to develop ACL 2.0.
I'm currently really busy with different library requests and other stuff.
I'll try to put my free time the ACL library, but note that my code is based on Erel's camera library. (which uses the same functions like mine) So i have no idea why some functions don't work for you (read: release, takepicture, etc).
I don't know if other people are encountering the same issue or not, but let me know if they do.

i'll get back to you as soon as i can.

Regards,
Tomas
 

fredo

Well-Known Member
Licensed User
Longtime User
Bluetooth permission in ACL library neccessary?

Hi XverhelstX,

vb1992 has pointed out to me that my newest app "flitztap" calls Bluetooth authorization, although BT is not really required.

Erel showed me that the ACL lib uses the BT permission.

I'm using ACL 4.6 and asked myself if it would be possible to remove the following lines from acl.xml, without endangering the functions of the otherwise excellent library.

B4X:
 <permission> android.permission.BLUETOOTH </ permission>
 <permission> android.permission.BLUETOOTH_ADMIN </ permission>

Regards,
fredo
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hi fredo,

I saw your app, nicely done.
As Erel declared, it are the objects that gives the permissions and not the libraries. (It is related to the AdvancedCamera instance).

Currently i'm completely rewritting AdvancedCamera to Advanced Camera Library 2.0 (not the version, but completely new.)

This means there will be more instances, and so less permissions for the library if you don't declare that needed instance. However I have no idea when I'm done with this library.

In the meanwhile, I guess you could use removepermission in your android manifest to delete those permissions.

Regards,
Tomas
 

pawelmic

Member
Licensed User
Longtime User
Hi, First of all, great Post, lot of info.

I have a question, I want to send an image (in byte array) with json, y have the image phisical, and in memory, but i don't know how to send it, any Idea?

I use
B4X:
bmpImage.Initialize(File.DirRootExternal,"1.jpg") and try to send 
query="<IMG>"& bmpImage &"</IMG>"
req.InitializePost2("http://190.1.1.0/recive_respuesta3_json.php", query.GetBytes("UTF8"))
But don't work it send some propertys of de images.


PD: sorry for my english is no very good

:cool: I used the HttpRequest object to send FORM data including an image to my server via PHP instead of JSON, you can later repackage all received data into JSON format on the server side if still needed. Go here to see the example and get the MultipartPost.bas:

http://www.b4x.com/forum/basic4android-getting-started-tutorials/8411-android-http-multipart-requests.html#post47094 :sign0188:
 

pawel2420

Member
Licensed User
Longtime User
Advanced Camera Library (4.6) - mirrored image

Hi all,

Function getPictureOnCurrentFrame returns mirrored image in the y-axis. Is it some bug in the library ?
 

Gsquared

Member
Licensed User
Longtime User
checking for cameras

Hi folks,

Does anyone know how to check what cameras a device has (rear and/or front), and whether it has a light? I would like to report a message to the user if one of these required functions is not present in their device.

Thanks,

G^2
 

ivanomonti

Expert
Licensed User
Longtime User
Size and Rotation - Help

Hello everyone, I'm trying to use the library ACL but no can do two simple things

1) Save image oriented as the shot, the image is always to -90
2) also because the image must be sent via the internet that I had a low weight

I tried to leave 0 as rotation and use ExifData but nothing ... who knows how to help me

Taken image

B4X:
Sub Camera1_PictureTaken (Data() As Byte)
   CameraSetting(Camera1)
   Camera1.rotateBitmap2(Data,0)
   Camera1.StartPreview
   Dim out As OutputStream
   Dim namepict As String
   namepict = DateTime.Date(DateTime.Now).Replace("/","").Replace(":","")
   DateTime.TimeFormat = "HHmmss"
   namepict = namepict & "_" & DateTime.Time(DateTime.Now).Replace("/","").Replace(":","")
   out = File.OpenOutput(File.DirRootExternal & "/mimmsos", namepict & ".jpg", False)
   out.WriteBytes(Data, 0, Data.Length)
   out.Close
   rotate_exif(File.Combine(File.DirRootExternal & "/mimmsos", namepict & ".jpg"),"")
   ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal & "/mimmsos", namepict & ".jpg"), True)
End Sub

Setting

B4X:
Sub CameraSetting(cam As AdvancedCamera)
      cam.Quality = 40
      cam.GPSLatitude = lat
      cam.GPSLongitude = lon
      cam.GPSAltitude = lat
      cam.OriPortrait
      cam.FlashAuto()
      cam.SceneMode = "INFINITY"
      cam.PictureSize(Panel2.Width,Panel2.Height)
End Sub

Rotate

B4X:
Sub rotate_exif(dir As String,fn As String)
    Dim ex As ExifData
    If File.Exists(dir,fn) Then
        ex.Initialize(dir,fn)
        ex.setAttribute("Orientation",ex.ORIENTATION_ROTATE_90)
        ex.saveAttributes
    End If        
End Sub

Thank
 

pawelmic

Member
Licensed User
Longtime User
to rotate and resize the picture

to rotate the picture you have to specify the angle BEFORE you take the picture NOT in picture_taken event:

B4X:
Sub btn_take_photo_Click
   camera1.TakePicture2(90) ' 90 is the angle to rotate
End Sub

to re-size the picture after it has been taken (before you send it) do this:

B4X:
If File.Exists(File.DirRootExternal, "test.jpg") Then
   If File.Size(File.DirRootExternal, "test.jpg") > 500000 Then
               
      Dim BMPinputStream As InputStream
      Dim bmp As Bitmap
      Dim out As OutputStream

      ' load the file into input stream:
      BMPinputStream = File.OpenInput(File.DirRootExternal, "test.jpg")
      bmp.Initialize2(BMPinputStream)
      BMPinputStream.Close 
               
      out = File.OpenOutput(File.DirRootExternal, "test.jpg", False)
      bmp.WriteToStream(out,60,"JPEG") '<- 60% low res JPEG
      out.Flush
      out.Close
                              
   End If
End If
 

peacemaker

Expert
Licensed User
Longtime User
Any help for error, described here (ACL also) ?

How to use the face camera in ACL 4.6 ?
"CameraFront" makes nothing, but "CameraFront2" gives error under ICS:

B4X:
LogCat connected to: "CKT TUNA"
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
main_camera1_ready (B4A line: 52)
camera1.CameraFront2
java.lang.RuntimeException: Fail to connect to camera service


   at android.hardware.Camera.native_setup(Native Method)
   at android.hardware.Camera.<init>(Camera.java:395)
   at android.hardware.Camera.open(Camera.java:357)
   at xvs.ACL.ACL.CameraFront2(ACL.java:337)
   at com.rootsoft.acl.main._camera1_ready(main.java:351)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:165)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:153)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:149)
   at xvs.ACL.ACL$2.surfaceCreated(ACL.java:157)
   at android.view.SurfaceView.updateWindow(SurfaceView.java:545)
   at android.view.SurfaceView.access$000(SurfaceView.java:81)
   at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
   at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:693)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1731)
   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2583)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4503)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Fail to connect to camera service
 
Last edited:

appie21

Active Member
Licensed User
Longtime User
Hello

i am new here i have a question about the Getpixel

i have load the basic tutorial of the advanced camera and it works
i want pixel color info from the live feed of my camera (camera1.StartPreview) in real time

for example i want the 20 pix from left and 20 pix from above
how to do

But i guess i must first convert it to a ImageView to use bitmap.GetPixel(20,20)
 

appie21

Active Member
Licensed User
Longtime User
ok i have do

i have now camerta 2.01 now i cant get Private r As Reflector
where an i reflector find?
 

coslad

Well-Known Member
Licensed User
Longtime User
Hi

Where is gone getSupportedPictureSize?

getSupportedPictureSize isn't supported in the 4.6 ver .

Now without getSupportedPictureSize how can is it possible to know the supported resolution ?

Thank !
 

coslad

Well-Known Member
Licensed User
Longtime User
HI ,
i solved writting this code :

B4X:
Dim indice As Int
Dim lista(50,2) As Int ' 50 is an arbitrary number 

Try 
For indice = 0 To 49 ' this is so high to make the  getSupportedPictureSizeWidth to go in error

lista(indice,0)=camera1.getSupportedPictureSizeWidth(indice)
lista(indice,1)=camera1.getSupportedPictureSizeheight(indice)
Next 

Catch
 indice=indice -1
End Try

msgbox(indice,"Number of Supported Picture Size is:")
 
Top