iOS Tutorial LLCamera - Low level camera access

Discussion in 'iOS Tutorials' started by Erel, Dec 28, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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.

    [​IMG]

    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:
    Code:
    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:
    Code:
    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: http://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:
    Code:
    #PlistExtra:<key>NSCameraUsageDescription</key><string>Taking a photo and attach to the task.</string>
     

    Attached Files:

    Last edited: Oct 18, 2016
    ilan and Hypnos like this.
  2. little3399

    little3399 Active Member Licensed User

    Hi, Erel
    If I want to save the img to disk , how to do ? Tks!
     
  3. ilan

    ilan Expert Licensed User

    try like this,

    Code:
    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,

    Code:
    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
     
    Mashiane likes this.
  4. little3399

    little3399 Active Member Licensed User

    Hi, ilan12041981

    Tks !
     
  5. Hypnos

    Hypnos Active Member Licensed User

    hi Erel and ilan12041981,

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

    Erel Administrator Staff Member Licensed User

    Ilan your answer is not correct. This is LLCamera object not Camera object.

    The correct answer:
    Code:
    Sub llc_PictureTaken (Data() As Byte)
      
    Dim out As OutputStream = File.OpenOutput(...)
      out.WriteBytes(Data, 
    0, Data.Length)
      out.Close
    End Sub
    Yes. Use Phone.AddImageToAlbum.
     
    Hypnos likes this.
  7. Hypnos

    Hypnos Active Member Licensed 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?
     

    Attached Files:

  8. Erel

    Erel Administrator Staff Member Licensed User

    The simplest solution is to create a new panel each time you initialize the camera. You will need to resize this panel in Page_Resize event.
     
    Hypnos likes this.
  9. Hypnos

    Hypnos Active Member Licensed 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??

    Code:
    Sub InitializeCamera(front As Boolean)
     
    If llc.IsInitialized Then llc.StopPreview
     PreviewPanel.RemoveAllViews
     camview.Initialize(
    "")
     PreviewPanel.AddView(camview, 
    0%x0%y100%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
     

    Attached Files:

  10. Erel

    Erel Administrator Staff Member Licensed User

    Call llc.Resize after you initialize llc.
     
    Hypnos likes this.
  11. MAGAREY

    MAGAREY Member Licensed User

    when i try to run the example i got this error:

    Code:
    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
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Have you added PlistExtra with NSCameraUsageDescription ?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice