Showing posts with label Camera. Show all posts
Showing posts with label Camera. Show all posts

How to Determine if Android Device has a Front Camera

public static final String FEATURE_CAMERA_FRONT was added in API level 9
Feature for getSystemAvailableFeatures() and hasSystemFeature(String).
The device has a front facing camera.
Constant Value: "android.hardware.camera.front"

1. Get a PackageManager Instance and save to myPackageManager.

2. Create a Boolean Variable named bolCameraFrontSupported

3. Set the Minimum and Target SDK version  to or greater. 

4. In the onCreate method, add the below code. This will return the Front Camera feature to a boolean named bolCameraFrontSupported. It will then display a toast displaying the Front Camera Feature on the device to the user.

bolCameraFrontSupported = myPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
if (bolCameraFrontSupported) {
Toast.makeText(this, "Device has Front Camera Feature", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No Front Camera Feature!", Toast.LENGTH_LONG).show();
}

5. Compile and run!

Resources:
http://developer.android.com/reference/android/content/pm/PackageManager.html#FEATURE_CAMERA_FRONT

How to Determine if Android Device has a Camera

public static final String FEATURE_CAMERA_ANY was added in API level 17
Feature for getSystemAvailableFeatures() and hasSystemFeature(String).
The device has at least one camera pointing in some direction.
Constant Value: "android.hardware.camera.any"

1. Get a PackageManager Instance and save to myPackageManager.

2. Create a Boolean Variable named bolCameraSupported

3. Set the Minimum and Target SDK version  to 17 or greater. 

4. In the onCreate method, add the below code. This will return the any camera support to a boolean named bolCameraSupported. It will then display a toast displaying the camera existence on the device to the user.

bolCameraSupported = myPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
if (bolCameraSupported) {
Toast.makeText(this, "Device has at least 1 Camera", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No Camera on Device!", Toast.LENGTH_LONG).show();
}

5. Compile and run!

Resources:
http://developer.android.com/reference/android/content/pm/PackageManager.html#FEATURE_CAMERA_ANY

FLASH_MODE_OFF - Turn Camera LED Flash Off

How to Turn Camera LED Flash On

* Note: This code may or may not work on some devices.
Works on Samsung Galaxy Player, Android 2.3.6, (API Level 10)

1. Create an Android project, if you don't already have one.
3. Add the below code in the onCreate method

p.setFlashMode(Parameters.FLASH_MODE_OFF);
myCamera.setParameters(p);
myCamera.stopPreview();

4. Compile and run!

Turn Camera LED Flash On

How to Turn Camera LED Flash On

* Note: This code may or may not work on some devices.
Works on Samsung Galaxy Player, Android 2.3.6, (Level 10)

1. Create an Android project, if you don't already have one.

2. Open AndroidManifest.xml and add the below line.

</manifest>
<uses-permission android:name="android.permission.CAMERA" />
    <application

3. Open MainActivity.java file and add the below to the imports section. 

import android.hardware.Camera;import  android.hardware.Camera.Parameters;

4. Add the below code between the public class and the onCreate method

public class MainActivity extends Activity{

private Camera camera;
 Parameters p;

    @Override
    public void onCreate(Bundle savedInstanceState)    {

5. Add the below code in the onCreate method

myCamera = Camera.open(); //needs import android.hardware.Camera;
p = myCamera.getParameters(); //needs import  android.hardware.Camera.Parameters;
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
myCamera.setParameters(p);
myCamera.startPreview();

6. Compile and run!

Resources:

How to Determine if Camera(facing away from screen) exists on an Android Device

public static final String FEATURE_CAMERA was added in API level 7
Feature for getSystemAvailableFeatures() and hasSystemFeature(String).
The device has a camera facing away from the screen.
Constant Value: "android.hardware.camera"

1. Get a PackageManager Instance and save to myPackageManager.

2. Create a Boolean Variable named bolCameraSupported

3. Set the Minimum and Target SDK version  to or greater. 

4. In the onCreate method, add the below code. This will return the camera support to a boolean named bolCameraSupported. It will then display a toast displaying the camera existence on the device to the user.

bolCameraSupported = myPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA);
if (bolCameraSupported) {
Toast.makeText(this, "Device has a Camera", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No Camera on Device!", Toast.LENGTH_LONG).show();
}

Determine if a Camera Exists on a Device and the Number of Cameras

How to Determine if a Camera Exists on a Device (# of cameras)

For API Level 9+

1. Create an Android project, if you don't already have one.

2. Open the MainActivity.java file and add the below code to the imports section.

import android.hardware.Camera;

3. In the MainActivity, add the below code to the onCreate method.

int numCameras = Camera.getNumberOfCameras(); // .getNumberOfCameras Added in API level 9
if (numCameras > 0) {
Toast.makeText(this, "This device has " + numCameras + " cameras", Toast.LENGTH_LONG).show(); //needs import android.widget.Toast;
} else {
Toast.makeText(this, "A camera does NOT exist on this device", Toast.LENGTH_LONG).show(); //needs import android.widget.Toast;
}
}

4. Compile and run!