iOS Tutorial LLCamera - Low level camera access

iMedia library v1.10 includes a new type of object named LLCamera. LLCamera provides low level camera access, similar to B4A Camera library. If you just want to allow the user to take a picture then it will probably be better to use Camera instead of LLCamera.

LLCamera allows you to embed the preview video in your layout, adjust the camera settings and it also allows you to work with the preview frames.

SS-2014-12-28_15.59.42.png


The first step is to initialize the LLCamera object. You need to pass a Panel. The preview video will be displayed on this panel.

Note that you need to call LLCamera.Resize whenever the panel is resized or the orientation changes.

The next step is to call LLCamera.StartPreview. The preview video will start and now you can call TakePicture to take a picture.
Unlike in B4A, you do not need to call StartPreview again after a picture is taken.

There are several "presets" which you can choose from by setting the Preset property:
B4X:
llc.Preset = llc.PRESET_640x80
These presets will change the image quality and size.

The following properties can only be changed inside a configuration block: FlashMode, Zoom and TorchMode.
See the ConfigureCamera sub in the attached example for more information.

The Preview event allows you to work with the preview frames. You must call ReleaseFrame at the end of this sub:
B4X:
Private Sub cam_Preview (Image As Bitmap)
   'work with the image
   '...
   cam.ReleaseFrame(Image)
End Sub
Otherwise new frames will not arrive.

An example of a CCTV client is also attached. The server code is available here: https://www.b4x.com/android/forum/threads/server-cctv-server.37382/#content

Starting from iOS 10 you need to add an explanation on the camera usage:
B4X:
#PlistExtra:<key>NSCameraUsageDescription</key><string>Taking a photo and attach to the task.</string>
 

Attachments

  • CCTV-Client-B4i.zip
    4.4 KB · Views: 932
  • LLCameraExample.zip
    3.4 KB · Views: 874
Last edited:

ilan

Expert
Licensed User
Longtime User
try like this,

B4X:
Sub Cam_Complete (Success As Boolean, Image As Bitmap, VideoPath As String)
    If Success Then
        If Image.IsInitialized Then
            camimage.Bitmap = Image 'put the takken photo to the imageview
        End If
    End If
End Sub

and now save the picture,

B4X:
Sub camok_Click

        Dim My_bmp As Bitmap
        My_bmp = camimage.Bitmap
        Dim out As OutputStream
        out = File.OpenOutput(File.DirDocuments, "Photo1.png", False)
        My_bmp.WriteToStream(out, 100, "png")
        out.Close

End Sub
 

Hypnos

Active Member
Licensed User
Longtime User
hi Erel and ilan12041981,

Is that possible to save the photo into Photos (Gallery) ? Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Ilan your answer is not correct. This is LLCamera object not Camera object.

The correct answer:
B4X:
Sub llc_PictureTaken (Data() As Byte)
  Dim out As OutputStream = File.OpenOutput(...)
  out.WriteBytes(Data, 0, Data.Length)
  out.Close
End Sub

Is that possible to save the photo into Photos (Gallery) ? Thanks!
Yes. Use Phone.AddImageToAlbum.
 

Hypnos

Active Member
Licensed User
Longtime User
Hi Erel,

I got a minor problem using this LLCamera Library, when "Preserve Ratio" is ON and switch between front/back camera, I got an old image show in the background (refer to the image). It's looks very strange... how can I fix it?
 

Attachments

  • Capture.JPG
    Capture.JPG
    47.1 KB · Views: 677

Hypnos

Active Member
Licensed User
Longtime User
Just tried it and I can remove the old background image but got another issue. When switch between front/back camera in landscape mode, the image orientation is not correct. anything incorrect in my code??

B4X:
Sub InitializeCamera(front As Boolean)
 If llc.IsInitialized Then llc.StopPreview
 PreviewPanel.RemoveAllViews
 camview.Initialize("")
 PreviewPanel.AddView(camview, 0%x, 0%y, 100%x , 100%y)
 llc.Initialize(camview, "llc", front)
 llc.StartPreview
 currentPreset = llc.Preset
 ConfigureCamera
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
 camview.Top = 0
 camview.Left = 0
 camview.Height = Height
 camview.Width = Width
 llc.Resize
End Sub
 

Attachments

  • Capture.JPG
    Capture.JPG
    49.1 KB · Views: 759

MAGAREY

Member
Licensed User
Longtime User
when i try to run the example i got this error:

B4X:
Application_Start
Error occurred on line: 51 (Main)
Error opening camera: Error Domain=AVFoundationErrorDomain Code=-11814 "Cannot Record" UserInfo={NSLocalizedDescription=Cannot Record, NSLocalizedRecoverySuggestion=Try recording again.}
Stack Trace: (
  CoreFoundation       __exceptionPreprocess + 171
  libobjc.A.dylib      objc_exception_throw + 48
  CoreFoundation       +[NSException raise:format:] + 197
  B4i Example          -[B4ILLCamera Initialize::::] + 735
  B4i Example          -[b4i_main _initializecamera:] + 955
  B4i Example          -[b4i_main _application_start:] + 1954
  CoreFoundation       __invoking___ + 140
  CoreFoundation       -[NSInvocation invoke] + 320
  B4i Example          +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1645
  B4i Example          -[B4IShell runMethod:] + 412
 B4i Example          -[B4IShell raiseEventImpl:method:args::] + 2487
 B4i Example          -[B4IShellBI raiseEvent:event:params:] + 1450
 B4i Example          __33-[B4I raiseUIEvent:event:params:]_block_invoke + 50
 libdispatch.dylib    _dispatch_call_block_and_release + 12
 libdispatch.dylib    _dispatch_client_callout + 8
 libdispatch.dylib    _dispatch_main_queue_callback_4CF + 1260
 CoreFoundation       __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
 CoreFoundation       __CFRunLoopRun + 2402
 CoreFoundation       CFRunLoopRunSpecific + 409
 GraphicsServices     GSEventRunModal + 62
 UIKit                UIApplicationMain + 159
 B4i Example          main + 111
 libdyld.dylib        start + 1
)
Application_Active
 

German Buchmuller

Member
Licensed User
Longtime User
I need to disable the sound effect when takepicuture is called. Is it possible?
 
Top