I want to create an application, and it needs to know when the camera is turned on by other app, whether the camera application is open or if a third party app is using the camera
how can i detect that if other app is try to open the camera . if other app try to open the camera i want to receive any broadcast before open the camera how can i do this.
i have also try this.
public boolean isCameraUsebyApp() {
Camera camera = null;
try {
camera = Camera.open();
} catch (RuntimeException e) {
return true;
} finally {
if (camera != null) camera.release();
}
return false;
}
but its alwys return true.
There is no broadcast for camera open or not but Camera Avaiability Listener in Camera2 Api, use below code
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
manager.registerAvailabilityCallback(new CameraManager.AvailabilityCallback() {
#Override
public void onCameraAvailable(String cameraId) {
super.onCameraAvailable(cameraId);
//Do your work
}
#Override
public void onCameraUnavailable(String cameraId) {
super.onCameraUnavailable(cameraId);
//Do your work
}
}, mHandler);
}
its only 21 and above
For below 21 you can use your code
public boolean isCameraInUse() {
Camera c = null;
try {
c = Camera.open();
} catch (RuntimeException e) {
return true;
} finally {
if (c != null) c.release();
}
return false;
}
Related
Problem background
I am developing a VR Project on Unreal Engine 4, and the project requires the usage of Android's native camera. Since there are no built-in functions in UE4 to iteract with Android's native methods, I customized this plugin under my need.
The original plugin uses the JNI interface to iteract with C++ code. It calls camera.open() and camera.startPreview() on UE4's EventBeginPlay, and calls camera.stopPreview() and camera.Release() on UE4's EventEndPlay. Since it is a known issue that EventEndPlay never fires up on Android platform, I decided to manipulate the camera in onResume() and onPause() methods. Here is the code:
<gameActivityClassAdditions>
<insert>
/* Unrelevant code goes here */
...
...
/* End of unrelevant code */
public void AndroidThunkJava_startCamera()
{
surfaceTexture = new SurfaceTexture(10);
surfaceTexture.setDefaultBufferSize(preferredWidth, preferredHeight);
if (camera == null){
try {
camera = Camera.open();
} catch (RuntimeException exc) {
return;
}
}
try {
camera.setPreviewTexture(surfaceTexture);
} catch (IOException t) {
return;
}
Parameters cameraParam = camera.getParameters();
cameraParam.setPreviewFormat(ImageFormat.NV21);
cameraParam.setPreviewSize(preferredWidth, preferredHeight);
cameraParam.setPreviewFpsRange(preferredFPS, preferredFPS);
cameraParam.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
if (cameraParam.isVideoStabilizationSupported()) {
cameraParam.setVideoStabilization(false);
}
if (cameraParam.isAutoWhiteBalanceLockSupported()) {
cameraParam.setAutoWhiteBalanceLock(false);
}
camera.setParameters(cameraParam);
camera.setPreviewCallback(new PreviewCallback() {
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
int Height = camera.getParameters().getPreviewSize().height;
int Width = camera.getParameters().getPreviewSize().width;
// calling C++ function via JNI interface
processFrameData(Width, Height, data);
}
});
camera.startPreview();
}
public void AndroidThunkJava_stopCamera()
{
if (camera != null)
{
camera.stopPreview();
camera.release();
camera = null;
}
}
</insert>
</gameActivityClassAdditions>
<gameActivityOnPauseAdditions>
<insert>
AndroidThunkJava_stopCamera();
</insert>
</gameActivityOnPauseAdditions>
<gameActivityOnResumeAdditions>
<insert>
AndroidThunkJava_startCamera();
</insert>
</gameActivityOnResumeAdditions>
The problem
The camera works fine every second time. That means:
I open the app, camera is working. I pushed the home button (which triggers onPause() method), then switch back to the app (triggers onResume() method). Pushed the home button again, and then switched back - camera works. And so on, the camera works every second time.
Anybody have any idea about this issue? Is that connected to the fact that android.hardware.Camera is deprecated? I'm using API version 19, so it is not possible to use newer android.hardware.camera2.
Here is my onStop and onResume methods. I'm not using onPause. And it works perfectly:
#Override
protected void onResume() {
super.onResume();
if (mCamera == null) {
restartPreview();
}
}
#Override
public void onStop() {
// stop the preview
if (mCamera != null) {
stopCameraPreview();
mCamera.release();
mCamera = null;
}
super.onStop();
}
private void restartPreview() {
if (mCamera != null) {
stopCameraPreview();
mCamera.release();
mCamera = null;
}
getCamera(mCameraID);
startCameraPreview();
}
private void startCameraPreview() {
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
setSafeToTakePhoto(true);
setCameraFocusReady(true);
} catch (IOException e) {
Log.d("st", "Can't start camera preview due to IOException " + e);
e.printStackTrace();
}
}
private void stopCameraPreview() {
setSafeToTakePhoto(false);
setCameraFocusReady(false);
// Nulls out callbacks, stops face detection
mCamera.stopPreview();
mPreviewView.setCamera(null);
}
maybe some implementations not equals yours but i think it is help you.
I'm creating a custom camera capturing videos with the new camera2 API.
My code is strongly inspired from the code provided by Google here. My camera preview has a button to switch from back to front camera and then from front to back camera.
The "camera preview" activity is launched with the back camera by default.
For some reason, when I click on the "switch/swap camera" button for the first time, it brings be to the front camera as it should, BUT everytime I click again, the switch/swap doesn't work anymore: the preview (on the front camera) fades a little bit, like if something is happening, but it remains on the currently selected (front) camera.
Here is my code :
In a RecordVideoFragment, in the onViewCreated:
// Switch camera button
switchCameraButton = (ImageButton) view.findViewById(R.id.button_switch_camera);
// Listener for Switch cameras button
switchCameraButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchCameras();
}
});
And here is the switchCameras() function:
private void switchCameras() {
mCameraOpenCloseLock.release();
mCameraDevice.close();
CameraManager mCameraManager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);
try {
String mCameraId = mCameraManager.getCameraIdList()[0];
if (mCameraId.equals("0")) { // If currently on FRONT camera (0 = CameraCharacteristics.LENS_FACING_FRONT)
mCameraId = "1"; // switch to BACK camera (1 = CameraCharacteristics.LENS_FACING_BACK)
switchCameraButton.setImageResource(R.drawable.ic_camera_front);
} else { // If currently on BACK camera
mCameraId = "0"; // switch to front camera
switchCameraButton.setImageResource(R.drawable.ic_camera_back);
}
try {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
mCameraManager.openCamera(mCameraId, mStateCallback, null);
} else {
requestVideoPermissions();
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
} catch (CameraAccessException e) {
Toast.makeText(getActivity(), "Cannot access the camera.", Toast.LENGTH_SHORT).show();
getActivity().finish();
}
}
If you have any idea on what's happening that would save me. I have been bugging on this for days. Thank you very much
What you need to do is introduce new variables:
public static final String CAMERA_FRONT = "1";
public static final String CAMERA_BACK = "0";
private String cameraId = CAMERA_BACK;
remove cameraId local variable from openCamera method.
public void switchCamera() {
if (cameraId.equals(CAMERA_FRONT)) {
cameraId = CAMERA_BACK;
closeCamera();
reopenCamera();
switchCameraButton.setImageResource(R.drawable.ic_camera_front);
} else if (cameraId.equals(CAMERA_BACK)) {
cameraId = CAMERA_FRONT;
closeCamera();
reopenCamera();
switchCameraButton.setImageResource(R.drawable.ic_camera_back);
}
}
public void reopenCamera() {
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
After looking at MrOnyszko answer i followed a slightly different approach:
In the Camera2Basic Tutorial a lens facing direction is used to set up the right camera, so i changed this direction before closing and reopening the camera.
private void switchCamera() {
if (mCameraLensFacingDirection == CameraCharacteristics.LENS_FACING_BACK) {
mCameraLensFacingDirection = CameraCharacteristics.LENS_FACING_FRONT;
closeCamera();
reopenCamera();
} else if (mCameraLensFacingDirection == CameraCharacteristics.LENS_FACING_FRONT) {
mCameraLensFacingDirection = CameraCharacteristics.LENS_FACING_BACK;
closeCamera();
reopenCamera();
}
}
private void reopenCamera() {
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
private void setUpCameraOutputs(int width, int height) {
Activity activity = getActivity();
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics characteristics
= manager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing != mCameraLensFacingDirection) {
continue;
}
...
I have been working in one of my android project in which I need to integrate Zbar scanner. I have integrated Zbar scanner in project. The problem I am facing is sometimes in Samsung Tab 3 has scanner area shows black screen. It is working perfectly in other devices. I have checked in nexus,canvas like devices and it shows perfect scanning screen. Is there any problem in my code? Here is my code.
private void initControls() {
try {
{
System.loadLibrary("iconv");
}
surfaceViewFlash = (SurfaceView) mView.findViewById(R.id.PREVIEW);
surfaceViewFlash.setVisibility(View.INVISIBLE);
surfaceViewFlash.setVisibility(View.VISIBLE);
mCameraScanner = getCameraInstance();
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
previewingScanner = true;
autoFocusHandlerScanner = new Handler();
try {
// Instance barcode zBarScanner
zBarScanner = new ImageScanner();
zBarScanner.setConfig(0, Config.X_DENSITY, 3);
zBarScanner.setConfig(0, Config.Y_DENSITY, 3);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
mPreviewScanner = new CameraPreview(getActivity(), mCameraScanner, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout) mView.findViewById(R.id.cameraPreview);
preview.addView(mPreviewScanner);
if (barcodeScanned) {
barcodeScanned = false;
mCameraScanner.setPreviewCallback(previewCb);
mCameraScanner.startPreview();
previewingScanner = true;
Log.e("initControls", "initControls");
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* A safe way to get an instance of the Camera object.
*/
public Camera getCameraInstance() {
Camera c = null;
int frontId = 0, backId = 0;
try {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
frontId = i;
} else if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
backId = i;
}
}
c = Camera.open(backId);
} catch (Exception e) {
e.printStackTrace();
GeneralAlertDialog.createDialog(getActivity(), getString(R.string.app_name), "Camera is not working, Please try again.", new DialogDismiss() {
#Override
public void onDismiss() {
getCallBackForCloseScanner().OnCloseButtonClickOfScanner();
}
});
}
return c;
}
private void releaseCamera() {
if (mCameraScanner != null) {
mCameraScanner.cancelAutoFocus();
previewingScanner = false;
mCameraScanner.setPreviewCallback(null);
mPreviewScanner.getHolder().removeCallback(mPreviewScanner);
mCameraScanner.stopPreview();
mCameraScanner.release();
mCameraScanner = null;
mPreviewScanner = null;
}
}
Runnable doAutoFocus = new Runnable() {
public void run() {
try {
if (previewingScanner) {
if (mCameraScanner == null)
mCameraScanner = getCameraInstance();
Camera.Parameters parameters = null;
Log.e("mCameraScanner", mCameraScanner + "");
if (null != mCameraScanner.getParameters()) {
parameters = mCameraScanner.getParameters();
}
List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCameraScanner.setParameters(parameters);
mCameraScanner.autoFocus(autoFocusCB);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
Please help me out to solve the problem.
releaseCamera on pause and on destroyed. it happen when camera is not release by any resource and you are again starting it.hope this will help you.
It's late, but this finally helped me, after overlooking the obvious.
Make sure you have camera permission for your app. It must be enabled not only through the camera permission declaration in the manifest file, BUT you also have to make sure you grant your app access to your camera by Android OS. Go to Preferences -> Apps -> Your App -> Permissions -> Allow camera access.
Starting from Android 6.0 (API 23), users are not asked for permissions at the time of installation rather developers need to request the permissions at the run time. Only the permissions that are defined in the manifest file can be requested at run time.
Request camera permission like here!.
In a previous way, flashlight feature could be used by using Camera class. But now that the entire Camera and Camera-related classes in android.hardware packages are deprecated, I should alternatively use some other classes in the android.hardware.camera2 package.
Traditionally, I coded the flashlight part like this.
// getting camera parameters
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
/*
* Turning On flash
*/
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
}
But now with the new API I'm getting so confused how to use the new one. Can anybody explain?
for flashlight I advise using the Camera2 API only from Android 6 (api 23), my function for toggling the flashlight looks like
#TargetApi(Build.VERSION_CODES.M)
public void toggleMarshmallowFlashlight(boolean enable) {
try {
final CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
final String[] list = manager.getCameraIdList();
manager.setTorchMode(list[0], enable);
} catch (CameraAccessException e) {
}
}
I am developing a very simple flashlight application, while I have successfully achieved what I was looking for, I would like to perform it that way I want it to. Currently my flashlight remains on while my activity is active, as soon as I hit the home button to minimize the activity flashlight turns off. I want the flashlight to stay on and turn off only when I click the turn off button in my activity.
I also want something like that if the flashlight is active and user hits the home button to minimize the activity, Turn Off button to be displayed in the notification bar.
Please guide me.
Please try to use bellow code.
public class CustomFlashLight {
private static CustomFlashLight instance;
private static Camera mCamera;
public static CustomFlashLight getInstance() {
if (null == instance) {
instance = new CustomFlashLight();
}
return instance;
}
public static boolean checkFlashAvailaibility(Context context) {
boolean flag = false;
try {
flag = context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH);
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public static boolean turnOnLight() {
boolean flag = false;
try {
mCamera = Camera.open();
if (mCamera != null) {
Parameters params = mCamera.getParameters();
if (Build.MODEL.equals("GT-P1000")) {
params.setFlashMode(Parameters.FLASH_MODE_ON);
} else {
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);
mCamera.startPreview();
mCamera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
flag = true;
} else {
flag = false;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public static boolean turnOffLight() {
boolean flag = false;
try {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
}
Got solution.
#Override
public void onBackPressed() {
super.onBackPressed();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
if (camera!= null) {
camera.release();
camera= null;
}
Log.d("Camera","Back Pressed");
}
And removed all the code from onStop() and onPause() method.