Front-Facing will darken when recording - android

The project is googlesamples/android-Camera2Video, and I changed the following code:
String[] cameraIds = manager.getCameraIdList();
String cameraId = cameraIds[0];
to
String[] cameraIds = manager.getCameraIdList();
String cameraId = cameraIds[0];
for (String id : cameraIds) {
Log.e(TAG, "openCamera id:" + id);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(id);
if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) {
cameraId = id;
break;
}
}
Now the camera is Front-Facing.
The Question :
Front-Facing will darken when recording.
The system Videotape cant find this issue, but Google Video sample can find (My project is to learn from sample).
It happens just on my company device (Phone).
I want to know if the problem is with the device or code?
Anybody can help me?
Thank you!

Related

Camera2 api characteristics.get(CameraCharacteristics.INFO_VERSION); getting null

I am trying to get camera version for my usb hardware camera using Camera2 api.I used characteristics.get(CameraCharacteristics.INFO_VERSION) i am getting null value. Is there any method to get the camera version or any other api so that i can get the version of my camera??.
private String getCameraVersion(Context context) {
String cameraVersion = "";
CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
try {
cameraId = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
cameraVersion = characteristics.get(CameraCharacteristics.INFO_VERSION);
} catch (CameraAccessException e) {
e.printStackTrace();
}
//Camera.getCameraInfo();
Log.i(TAG, "Camera version " + cameraVersion);
return cameraVersion;
}
INFO_VERSION is an optional string that some manufacturers might set, primarily to include within the EXIF TAG_SOFTWARE field of any JPEG images an app might produce. It's there just to help with debugging, and the manufacturers that include it often include some kind of information about hardware revisions or firmware versions. But it's not standardized in any way.
The default USB camera HAL for Android doesn't set that field to anything, so it's not surprising it's null.

How to open usb camera in android using android.hardware.camera.external

I have connected a usb camera and want open and take picture.
I am trying to figure how to use "android.hardware.camera.external" feature for accessing that camera.
The "manager.getCameraIdList();" returns count of 2 camera(for front and back) and not the usb camera that is connected.
For me works the next config for USB Cameras with official API:
Camera (old API)
On some china devices, Camera.CameraInfo.CAMERA_FACING_FRONT works, but some times your should force to camIdx to 0.
public static Camera getCameraInstance() {
Camera c = null;
try {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
Log.i(TAG, "[Camera] try to open camera camIdx:" + camIdx);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
c = Camera.open(camIdx);
mCameraIdx = camIdx;
Log.i(TAG, "[Camera] camIdx:" + camIdx);
} catch (RuntimeException e) {
Log.e(TAG, "[Camera] failed to open: " + e.getLocalizedMessage());
}
}
}
if (c == null) {
Log.i(TAG, "[Camera] forcing open camera with camIdx 0");
c = Camera.open(0); // force because FACING_FRONT not found
mCameraIdx = 0;
}
} catch (Exception e) {
Logger.e("TAG", "[Camera] Open camera failed: " + e);
}
return c;
}
Camera2 (new API)
Similar for Camera2 API:
public String getCamera(CameraManager manager) {
String cameraIndex = "0";
try {
for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
Logger.d(TAG, "cameraId " + cameraId);
int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING);
if (cOrientation != CAMERACHOICE) {
cameraIndex = cameraId;
}
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
return cameraIndex;
}
But in my experience some Android ROM implementations are really bad (sometimes custom hardware or china hardware) then it not works. In some situations you should be use libuvc driver fore some USB Cameras: https://github.com/saki4510t/UVCCamera
There's currently (as of Android O) no common USB camera support on Android devices via the standard camera API.
Some Android manufacturers do have their own support for USB cameras, but it's hard to know what devices do and what don't.

Why is the camera flash not turning on and the CameraAccessException class is underlined red?

I am trying to make a torchlight app but I am not able to turn it on properly. I have used the following logic to turn it on. Please let me know where I am going wrong. When I run this on my android phone it runs properly but the flashlight doesn't start.
if (count[0] == 0) {
count[0] = 1;
((TransitionDrawable) imageView.getDrawable()).startTransition(3000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String cameraId = null; // Usually back camera is at 0 position.
try {
cameraId = camManager.getCameraIdList()[0];
camManager.setTorchMode(cameraId, true); //Turn ON
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
} else {
count[0] = 0;
((TransitionDrawable) imageView.getDrawable()).reverseTransition(3000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
CameraManager camManager1 = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String cameraId = null; // Usually back camera is at 0 position.
try {
cameraId = camManager1.getCameraIdList()[0];
camManager1.setTorchMode(cameraId, false); //Turn ON
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
The code you have written is fully functional on Android Marshmallow+.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
This line checks for what version of Android you are running the app on. It executes the code within the if block only on Android version 6+ (API level 23+).
You need to add the else part with an older way of turning the flashlight on like this method: How to turn on camera flash light programmatically in Android?

Switch front and back camera2 in android

I am creating a camera in android using android.hardware.camera2...
I referred to this link
But, I do not know how to switch front camera and back camera.
Please give a advice!
First of all get the list of camera Ids from device
We can use CameraManager to iterate all the cameras that are available in the system, each with a designated cameraId. Using the cameraId, we can get the properties of the specified camera device. Those properties are represented by class CameraCharacteristics. Things like "is it front or back camera", "output resolutions supported" can be queried there.
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
return manager.getCameraIdList();
} catch (CameraAccessException e) {
return null;
}
Now if you want to open Front camera
CameraCharacteristics characteristics
= manager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
//Do your code here (open Camera with Camera ID)
}
This is another method directly which return directly CameraId .
String getFrontFacingCameraId(CameraManager cManager){
for(final String cameraId : cManager.getCameraIdList()){
CameraCharacteristics characteristics = cManager.getCameraCharacteristics(cameraId);
int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING);
if(cOrientation == CameraCharacteristics.LENS_FACING_FRONT) return cameraId;
}
return null;
}
For More Information about Camera2 Api you can see Here

How to detect no of camera's available in android device? and also if the device has front camera how to use it?

How to detect no of camera's available in android device? and also if the device has front camera how to use it?
What I would suggest is similar to doc_180's answer, but should be able to detect both front and back facing cameras even for Froyo, though if I'm not mistaken, Froyo never supported front-facing cameras, so you'll always get a false response for frontCam on Froyo.
PackageManager pm = getPackageManager();
boolean frontCam, rearCam;
//Must have a targetSdk >= 9 defined in the AndroidManifest
frontCam = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
rearCam = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
EDIT: Just realized this is a really, really old question. Oh well, hopefully it helps someone in the future.
Use packagemanager to check if the device supports the Intent. In this case Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
}
}
The quickest way I've found to check if a (backfacing) camera exists is to check if Camera.open() returns null.
Camera cam = Camera.open();
if(null == cam){
//no camera exists
}
This should be available for earlier versions of android as well.
you can use this static method if you just want to know how many cameras there are:
Camera.getNumberOfCameras(); (api 9)
/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
I am using this method to get the count of the available camera
public int getCameraCount(){
CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
try {
String[] strings = manager.getCameraIdList();
return strings.length;
} catch (CameraAccessException e) {
e.printStackTrace();
return 0;
}
}
Try this, this worked for me in a Moto RAZR HD:
public static Camera open (int cameraId)
Example usage:
mCamera = Camera.open(1);

Categories

Resources