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: 1,953
  • ACL4.5.zip
    33.5 KB · Views: 1,396
  • ACL4.6.zip
    359.4 KB · Views: 4,880
Last edited:

jur123

Member
Licensed User
Longtime User
Portrait (bitmap rotated 90 deg)

Hi,

I have been struggeling with portait for some days now. Portait does work, but the resulting bitmap are rotated 90 degrees (CCW).

How can I fix that?

Thxm Jur
:D
 

jur123

Member
Licensed User
Longtime User
advanced camera portrait gives rotated bitmap/jpg

Hi Erel,

Thx for your answer, but the process you pointed out of saving the byte() from 'picture_taken' to file, loading the bitmap from file, rotating it and then resaving it, is too slow for my purpose.
Is there a (simple) way to rotate (90 deg) the contents of the byte() array given bij picture_taken?
Or is there a function loading (in memory) a bitmap from byte() ? E.g "loadbitmapfrombytes()" so I can rotate the bitmap before saving...

Thx for any help

Jur
 

slydog43

Member
Licensed User
Longtime User
flash question

I'm a little confused about using the flash. I made a very simple flashlight app using this library (4.3). My problem lies with having to use camera.FlashTorch to turn on the flash and camera.FlashOff to turn off. Why doesn't camera.FlashOn work, or is this something to do with my phone (a t-mobile MyTouch 4G, rooted, etc).

I have tried my code on a Samsung Fascinate, but does nothing. Maybe related?


thanks
 

jfr76

New Member
Licensed User
Longtime User
Null pointer thrown when leaving activity

Hello, I'm using ACL 4.3, and if I hit the 'home' button from my activity (which displays the camera preview), I get a null pointer exception 50% of the time. My Activity_Pause looks like so:

PHP:
Sub Activity_Pause (UserClosed As Boolean)
       released = True
       camera1.StopPreview 
   camera1.Release
   
   GPS1.Stop 
   locAvail = False
   ToastMessageShow("paused", True)
End Sub

the toast message is NOT shown when I get the crash, which according to the logs in the IDE looks like:

PHP:
** Activity (main) Pause, UserClosed = false **
dst width, height have changed [w= 462, h= 693] -> [w=456, h= 693]
dst width, height have changed [w= 441, h= 662] -> [w=440, h= 662]
dst width, height have changed [w= 430, h= 644] -> [w=424, h= 644]
GC_CONCURRENT freed 2051K, 46% free 5881K/10823K, external 4925K/5669K, paused 4ms+8ms
dst width, height have changed [w= 424, h= 636] -> [w=424, h= 636]
dst width, height have changed [w= 331, h= 497] -> [w=328, h= 497]
virtual android::status_t android::CameraHardwareSec::cancelPicture() : not supported, just returning NO_ERROR
DeinitCamera: m_cam_fd(14)
overlay_destroyOverlay:IN dev (0x298428) and overlay (0x692ce8)
Destroying overlay/fd=214/obj=00692ce8
overlay_destroyOverlay:OUT
DeinitCamera: m_cam_fd2(15)
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40015560)
FATAL EXCEPTION: main
java.lang.NullPointerException
   at xvs.ACL.ACL.onPreviewFrame(ACL.java:1090)
   at android.hardware.Camera$EventHandler.handleMessage(Camera.java:547)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3647)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)
GC_EXPLICIT freed 2K, 53% free 2535K/5379K, external 1625K/2137K, paused 71ms

Am I doing something wrong when stopping the preview and releasing the camera? has anyone else seen this?
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
@jfr76: Sorry, I have no idea why this happens.

However: I updated acl to 4.4

This adds a new functions:

B4X:
camera1.TakePicture2(degrees)

you should use this when you try to take a picture as portrait.
As degrees, give 90 to have a portrait picture.


code used:

Raw data:

B4X:
Sub mnuTakePicture2_Click
   
   camera1.TakePicture2(90)
   
End Sub

Sub Camera1_PictureTaken2 (Data() As Byte)

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

   
End Sub
 

jfr76

New Member
Licensed User
Longtime User
A fix for the null pointer error

I was able to correct this issue by modifying the source and compiling my own version with the change. I started with 4.4
Specifically, I had to change onPreviewFrame to this :
PHP:
@Override
   public void onPreviewFrame(byte[] data, Camera camera) {
      if (c != null && isPreviewOn) {  // verify we're running and not a trailing event to a dead camera obj
         Camera.Parameters cp = c.getParameters();
         Size sz = cp.getPreviewSize();
         int width = sz.width; //FrameWidth;
         int height = sz.height; //FrameHeight;
         int quality = FrameQuality;

           int[] temp = new int[width*height];
           OutputStream outptstream = new ByteArrayOutputStream();
           Bitmap bm = null;
           
           decodeYUV420SP(temp, data, width, height);
           bm = Bitmap.createBitmap(temp, width, height, Bitmap.Config.RGB_565);
           bm.compress(CompressFormat.JPEG, quality, outptstream);
           CurrentJPEG = ((ByteArrayOutputStream)outptstream).toByteArray();
         isDecoding = false;
            Log.i("B4A", "isDecoding false");
          ba.raiseEvent(this, eventName + "_previewtaken", new Object[] { CurrentJPEG } );
      }
}
 

mkvidyashankar

Active Member
Licensed User
Longtime User
Hi,
I tried the following code but it does not seem to work for some reason (device: Motorola MB525) - does nothing. MaxZoom is returned as 5, but setting it to any value does nothing at all. Obviously I am doing something wrong ...

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
Select KeyCode
Case KeyCodes.KEYCODE_VOLUME_DOWN:
maxZoom = cam.getMaxZoom
If CamZoom < maxZoom Then
CamZoom = CamZoom + 1
cam.setZoom = CamZoom
End If
Return True
Case KeyCodes.KEYCODE_VOLUME_UP:
If CamZoom > 0 Then
CamZoom = CamZoom - 1
cam.setZoom = CamZoom
End If
Return True
End Select
End Sub


hi

I have tried the code similar to above but i did not get any zoom effect. Please let me know u got the solution. Thanks in advance.
 
I get a java error everytime I try to start the led light

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 btnFLASHLIGHT As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
   End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
End Sub



Sub btnFLASHLIGHT_Click
   camera1.FlashTorch()
End Sub
 

Gigatron

Member
Licensed User
Longtime User
B4X:
Sub btnFLASHLIGHT_Click
   camera1.FlashTorch()
End Sub

Wrong code.. FlashTorch()

It's be camera1.FlashOn , this will work without error.

Good Luck
 

francoisg

Active Member
Licensed User
Longtime User
Hi, on the Motorola MB525 (Defy) I can't get the flashlight to work at all (same with Galaxy Tab). Any reason for this?

It works fine with a lot of other devices (SE X10, Samsung Galaxy SII etc...)

The "FlashTorch" functions just seems to do nothing even though after calling it "CurrentFlashMode" seems to report the expected state correctly...
 

Gigatron

Member
Licensed User
Longtime User
Here the flashlight code working on my SEX10i

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 Camera As AdvancedCamera
   Dim campanel As Panel
   Dim showbtn As Button
   Dim bton As Button
    
End Sub

Sub Activity_Create(FirstTime As Boolean)

      Activity.LoadLayout("camer")
   Camera.Initialize(campanel, "Camera")
  
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   Camera.StopPreview
   
End Sub
 

Sub Camera_Ready (Success As Boolean)

   If success Then
      Camera.StartPreview
      
      Else
      ToastMessageShow("Cannot open camera.", True)
   End If
   
End Sub


Sub bton_Click
 
   Camera.FlashOn
   
   
End Sub
 

gpe

Member
Licensed User
Longtime User
I cannot find a way to enable FlashOn and other camera functions on GalaxyTab 7.
No B4a tutorial seems to work, even if the responses are OK.
Other camera standard applications works instrad perfectly.
Any suggestion?
gp
 

SlavaVB

Member
Licensed User
Longtime User
I'm having issues turning the FLASH on...

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 Cam1 As AdvancedCamera
End Sub

Sub Activity_Create(FirstTime As Boolean)
   cam1.Initialize(Activity, "cam")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub cam_Ready (Success As Boolean)

   Msgbox("on!","")
   
   cam1.FlashOn()

End Sub


Do you know whats wrong?
 
Top