Android Example Have Back and Front Camera's & has Flash (inline Java code)

This is just some simple inline Java code to check if a device has a Back facing camera, a Front facing camera, and a Flash (it requires the JavaObject library to be enabled).

1.png


B4X:
#Region  Project Attributes
    #ApplicationLabel: HasFrontBackCameraAndFlash
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Dim nativeMe As JavaObject
   
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.

   
    Private Button1 As Button
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
   
    nativeMe.InitializeContext

   
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
   
    'if the device has a back camera only then the ID's will be BACK = 0 and FRONT = -1
    'if the device has a front camera only then the ID's will be BACK = -1 and FRONT = 0
    'if the device has a back and front camera then the ID's will be BACK = 0 and FRONT = 1
    Log("ID of Front Facing Camera = " & nativeMe.RunMethod("getFrontFacingCamera", Null))
    Log("ID of Back Facing Camera = " & nativeMe.RunMethod("getBackFacingCamera", Null))
    Log("Device has a flash = " & nativeMe.RunMethod("hasFlash", Null))
   
    Label1.Text = "   ID of Front Facing Camera = " & nativeMe.RunMethod("getFrontFacingCamera", Null) & Chr(10) & _
                  "   ID of Back Facing Camera = " & nativeMe.RunMethod("getBackFacingCamera", Null) & Chr(10) & _
                  "   Device has a flash = " & nativeMe.RunMethod("hasFlash", Null)
   
End Sub


#If Java

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.CameraInfo;
import android.content.pm.PackageManager;
import android.content.Context;


    public int CAMERA_FRONT = -1;
    public int CAMERA_BACK = -1;   
    private boolean hasflash = false;

    public int getFrontFacingCamera() {
        int cameraId = -1;
        // Search for the front facing camera
        // get the number of cameras       
        int numberOfCameras = Camera.getNumberOfCameras();
        // for every camera check       
        for (int i = 0; i < numberOfCameras; i++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                cameraId = i;
                break;
            }
        }
        CAMERA_FRONT = cameraId;
        return cameraId;
    }

    /**
     * Get the ID of the back facing camera
     * If the device has a front and back camera then the ID = 0
     * If the device has a back facing camera only then the ID = 0
     * It will return the ID as -1 if there is no back facing camera
     */
    public int getBackFacingCamera() {
        int cameraId = -1;
        // Search for the back facing camera
        // get the number of cameras
        int numberOfCameras = Camera.getNumberOfCameras();
        // for every camera check
        for (int i = 0; i < numberOfCameras; i++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
                cameraId = i;
                break;
            }
        }
        CAMERA_BACK = cameraId;
        return cameraId;
    }   
   
    public boolean hasFlash() {
        /*
         * First check if device is supporting a flashlight or not
         */
        hasflash = BA.applicationContext.getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        
         return hasflash;
    }   

#End If
 

Attachments

  • b4aHasFrontBackCameraAndFlash.zip
    8.8 KB · Views: 762

Juan Marrero

Active Member
Licensed User
Longtime User
Maybe you can guide me. I can only add functions in Inline Java Code, I can't add full classes. Let me explain. I found a java code for opening both cameras at the same time for my htc one m8, but they are in 3 sepparate classes (BackCameraPreview.java, FrontCameraPreview.java and DualCamActivity.java). I copied all 3 codes inside same "#IF JAVA" statement and it always errors (Illegal static declaration is one of them).
 

Johan Schoeman

Expert
Licensed User
Longtime User
Maybe you can guide me. I can only add functions in Inline Java Code, I can't add full classes. Let me explain. I found a java code for opening both cameras at the same time for my htc one m8, but they are in 3 sepparate classes (BackCameraPreview.java, FrontCameraPreview.java and DualCamActivity.java). I copied all 3 codes inside same "#IF JAVA" statement and it always errors (Illegal static declaration is one of them).
Try with the attached B4A project and B4A library files. Copy the library files to your additional library folder. I can't test it on my devices as their hardware does not support a dual camera operation. But from what I have read it seems to me as if the HTC One M8 does support dual camera operation (so, I am working blind here at present). On my devices it starts the DualCamActivity but crashes once it started the back camera and then tries to start the front camera. I can however start the cameras on my devices individually if I manipulate the code so that either the front or the back camera are used and not both together.

If it works on your HTC then the camera views should be side by side and in landscape. Use your device's back button to stop the previews.

There are a number of files in the following folders of the B4A project(make sure that once you have downloaded the B4A project and unzipped it that all the files in these folders are set to READ ONLY):
/Objects/res/values
/Objects/res/layout
Objects/res/menu

If it crashes then please post the error log...
 

Attachments

  • DualCameraLibFiles.zip
    8.5 KB · Views: 583
  • b4aAndroidDualCamera.zip
    13.8 KB · Views: 641
Last edited:

Juan Marrero

Active Member
Licensed User
Longtime User
WOW!!! Worked out of the box!! What else can this lib do? All I want is to mimic the Android Kitkat 4.4.2 HTC Camera app when it had a Dual Camera option where one of the cameras was full screen and the other was on a small movable rectangle.
 

Johan Schoeman

Expert
Licensed User
Longtime User
WOW!!! Worked out of the box!! What else can this lib do? All I want is to mimic the Android Kitkat 4.4.2 HTC Camera app when it had a Dual Camera option where one of the cameras was full screen and the other was on a small movable rectangle.
Nothing else yet - needed to know if it starts both cameras...
 

Juan Marrero

Active Member
Licensed User
Longtime User
Yes it worked. My co-worker have an iPhone and I showed him both cameras opened at the same time and he was amazed. Looks exactly as the HTC Camera App "Split Screen" feature.
 
Top