Java Code Examples for android.hardware.Camera#getNumberOfCameras()

The following examples show how to use android.hardware.Camera#getNumberOfCameras() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Camera1Manager.java    From phoenix with Apache License 2.0 8 votes vote down vote up
@Override
public void initializeCameraManager(CameraConfigProvider cameraConfigProvider, Context context) {
    super.initializeCameraManager(cameraConfigProvider, context);

    mNumberOfCameras = Camera.getNumberOfCameras();

    for (int i = 0; i < mNumberOfCameras; ++i) {
        final Camera.CameraInfo cameraInfo = new Camera.CameraInfo();

        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            mFaceBackCameraId = i;
            mFaceBackCameraOrientation = cameraInfo.orientation;
        } else if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            mFaceFrontCameraId = i;
            mFaceFrontCameraOrientation = cameraInfo.orientation;
        }
    }
}
 
Example 2
Source File: CameraManager.java    From AudioVideoCodec with Apache License 2.0 6 votes vote down vote up
/**
 * 切换摄像头
 */
public void switchCamera() {
    int cameraCount = Camera.getNumberOfCameras();
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    for (int i = 0; i < cameraCount; i++) {
        Camera.getCameraInfo(i, cameraInfo);
        if (defaultCameraId == 0) {
            //后置切前置
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                restartCameraPreview(i);
                defaultCameraId = i;
                break;
            }
        } else {
            //前置切后置
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                restartCameraPreview(i);
                defaultCameraId = i;
                break;
            }
        }
    }
}
 
Example 3
Source File: SurfaceGrabberActivity.java    From Zom-Android-XMPP with GNU General Public License v3.0 6 votes vote down vote up
private boolean tryCreateCamera(int facing) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    for (int nCam = 0; nCam < Camera.getNumberOfCameras(); nCam++) {
        Camera.getCameraInfo(nCam, info);
        if (info.facing == facing) {
            camera = Camera.open(nCam);
            cameraInfo = info;
            //Size size = choosePictureSize(camera.getParameters().getSupportedPictureSizes());

            Camera.Parameters params = camera.getParameters();
            params.setPictureFormat(ImageFormat.JPEG);
            //params.setPictureSize(size.width,size.height);
            //params.setJpegThumbnailSize(128,128);
            //params.setPreviewSize(size.width/2,size.height/2);

            if (this.getCameraDirection() == CameraInfo.CAMERA_FACING_BACK) {
                params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
            }

            camera.setParameters(params);

            return true;
        }
    }
    return false;
}
 
Example 4
Source File: RCTCamera.java    From react-native-camera-face-detector with MIT License 6 votes vote down vote up
private RCTCamera(int deviceOrientation) {
    _cameras = new HashMap<>();
    _cameraInfos = new HashMap<>();
    _cameraTypeToIndex = new HashMap<>();

    _actualDeviceOrientation = deviceOrientation;

    // map camera types to camera indexes and collect cameras properties
    for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT && _cameraInfos.get(RCTCameraModule.RCT_CAMERA_TYPE_FRONT) == null) {
            _cameraInfos.put(RCTCameraModule.RCT_CAMERA_TYPE_FRONT, new CameraInfoWrapper(info));
            _cameraTypeToIndex.put(RCTCameraModule.RCT_CAMERA_TYPE_FRONT, i);
            acquireCameraInstance(RCTCameraModule.RCT_CAMERA_TYPE_FRONT);
            releaseCameraInstance(RCTCameraModule.RCT_CAMERA_TYPE_FRONT);
        } else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK && _cameraInfos.get(RCTCameraModule.RCT_CAMERA_TYPE_BACK) == null) {
            _cameraInfos.put(RCTCameraModule.RCT_CAMERA_TYPE_BACK, new CameraInfoWrapper(info));
            _cameraTypeToIndex.put(RCTCameraModule.RCT_CAMERA_TYPE_BACK, i);
            acquireCameraInstance(RCTCameraModule.RCT_CAMERA_TYPE_BACK);
            releaseCameraInstance(RCTCameraModule.RCT_CAMERA_TYPE_BACK);
        }
    }
}
 
Example 5
Source File: CameraSource.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
/**
 * Gets the id for the camera specified by the direction it is facing.  Returns -1 if no such
 * camera was found.
 *
 * @param facing the desired camera (front-facing or rear-facing)
 */
private static int getIdForRequestedCamera(int facing) {
    CameraInfo cameraInfo = new CameraInfo();
    for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == facing) {
            return i;
        }
    }
    return -1;
}
 
Example 6
Source File: FuTextureCamera.java    From FuAgoraDemoDroid with MIT License 5 votes vote down vote up
private void openCamera(int cameraFacing) {
        synchronized (mCameraLock) {
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            int numCameras = Camera.getNumberOfCameras();

            for (int i = 0; i < numCameras; ++i) {
                Camera.getCameraInfo(i, cameraInfo);
                if (cameraInfo.facing == cameraFacing) {
                    mCamera = Camera.open(i);
                    mCameraFacing = cameraFacing;
                    break;
                }
            }

            if (mCamera == null) {
                Log.d(TAG, "No front-facing mCamera found; opening default");
                mCamera = Camera.open();
            }

            if (mCamera == null) {
                throw new RuntimeException("Unable to open camera");
            } else {
                Camera.Parameters parms = mCamera.getParameters();
//                List<int[]> frameRates = parms.getSupportedPreviewFpsRange();
//                int minFps = frameRates.get(frameRates.size() - 1)[0];
//                int maxFps = frameRates.get(frameRates.size() - 1)[1];
//                parms.setPreviewFpsRange(minFps, maxFps);
                parms.setPreviewSize(mWidth, mHeight);
//                parms.setRecordingHint(true);
                mCamera.setParameters(parms);
                Camera.Size cameraPreviewSize = parms.getPreviewSize();
                String previewFacts = cameraPreviewSize.width + "x" + cameraPreviewSize.height;
                int deviceOrientation = getDeviceOrientation();
                mCameraOrientation = getFrameOrientation(cameraInfo, deviceOrientation);
                Log.i(TAG, "open Camera config: " + previewFacts + ", orientation:" + mCameraOrientation);
            }
        }
    }
 
Example 7
Source File: VideoStream.java    From VideoMeeting with Apache License 2.0 5 votes vote down vote up
public void switchCamera() throws RuntimeException, IOException {
        if (Camera.getNumberOfCameras() == 1) throw new IllegalStateException("Phone only has one camera !");
        boolean streaming = mStreaming;
//        boolean previewing = mCamera!=null && mCameraOpenedManually;
        boolean previewing = mCamera!=null;
        mCameraId = (mCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) ? Camera.CameraInfo.CAMERA_FACING_FRONT : Camera.CameraInfo.CAMERA_FACING_BACK;
        setCamera(mCameraId);
        stopPreview();
//        mFlashEnabled = false;
        if (previewing) startPreview();
        if (streaming) start();
    }
 
Example 8
Source File: CameraSource.java    From AndroidApp with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Gets the id for the camera specified by the direction it is facing.  Returns -1 if no such
 * camera was found.
 *
 * @param facing the desired camera (front-facing or rear-facing)
 */
private static int getIdForRequestedCamera(int facing) {
    CameraInfo cameraInfo = new CameraInfo();
    for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == facing) {
            return i;
        }
    }
    return -1;
}
 
Example 9
Source File: VideoStream.java    From spydroid-ipcamera with GNU General Public License v3.0 5 votes vote down vote up
/**	Switch between the front facing and the back facing camera of the phone. 
 * If {@link #startPreview()} has been called, the preview will be  briefly interrupted. 
 * If {@link #start()} has been called, the stream will be  briefly interrupted.
 * You should not call this method from the main thread if you are already streaming. 
 * @throws IOException 
 * @throws RuntimeException 
 **/
public void switchCamera() throws RuntimeException, IOException {
	if (Camera.getNumberOfCameras() == 1) throw new IllegalStateException("Phone only has one camera !");
	boolean streaming = mStreaming;
	boolean previewing = mCamera!=null && mCameraOpenedManually; 
	mCameraId = (mCameraId == CameraInfo.CAMERA_FACING_BACK) ? CameraInfo.CAMERA_FACING_FRONT : CameraInfo.CAMERA_FACING_BACK; 
	setCamera(mCameraId);
	stopPreview();
	mFlashEnabled = false;
	if (previewing) startPreview();
	if (streaming) start(); 
}
 
Example 10
Source File: CameraToMpegTest.java    From Android-MediaCodec-Examples with Apache License 2.0 5 votes vote down vote up
/**
 * Configures Camera for video capture.  Sets mCamera.
 * <p>
 * Opens a Camera and sets parameters.  Does not start preview.
 */
private void prepareCamera(int encWidth, int encHeight) {
    if (mCamera != null) {
        throw new RuntimeException("camera already initialized");
    }

    Camera.CameraInfo info = new Camera.CameraInfo();

    // Try to find a front-facing camera (e.g. for videoconferencing).
    int numCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numCameras; i++) {
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            mCamera = Camera.open(i);
            break;
        }
    }
    if (mCamera == null) {
        Log.d(TAG, "No front-facing camera found; opening default");
        mCamera = Camera.open();    // opens first back-facing camera
    }
    if (mCamera == null) {
        throw new RuntimeException("Unable to open camera");
    }

    Camera.Parameters parms = mCamera.getParameters();

    choosePreviewSize(parms, encWidth, encHeight);
    // leave the frame rate set to default
    mCamera.setParameters(parms);

    Camera.Size size = parms.getPreviewSize();
    Log.d(TAG, "Camera preview size is " + size.width + "x" + size.height);
}
 
Example 11
Source File: CaptureVideoActivity.java    From NIM_Android_UIKit with MIT License 5 votes vote down vote up
@TargetApi(9)
private void switchCamera() {
    if (Build.VERSION.SDK_INT >= 9) {
        cameraId = (cameraId + 1) % Camera.getNumberOfCameras();
    }
    resizeSurfaceView();
    shutdownCamera();
    initCamera();
    startPreview();
}
 
Example 12
Source File: Camera1.java    From cameraview with Apache License 2.0 5 votes vote down vote up
/**
 * This rewrites {@link #mCameraId} and {@link #mCameraInfo}.
 */
private void chooseCamera() {
    for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) {
        Camera.getCameraInfo(i, mCameraInfo);
        if (mCameraInfo.facing == mFacing) {
            mCameraId = i;
            return;
        }
    }
    mCameraId = INVALID_CAMERA_ID;
}
 
Example 13
Source File: CameraSource.java    From Document-Scanner with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the id for the camera specified by the direction it is facing.  Returns -1 if no such
 * camera was found.
 *
 * @param facing the desired camera (front-facing or rear-facing)
 */
private static int getIdForRequestedCamera(int facing) {
    CameraInfo cameraInfo = new CameraInfo();
    for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == facing) {
            return i;
        }
    }
    return -1;
}
 
Example 14
Source File: CameraViewProxy.java    From Ti-Android-CameraView with MIT License 5 votes vote down vote up
/**
 * Function to determine if a front camera exists
 * @return Boolean Front Camera Exists
 */

private boolean hasFrontCamera(){
	int numCameras= Camera.getNumberOfCameras();
	for(int i=0;i<numCameras;i++){
	    Camera.CameraInfo info = new CameraInfo();
	    Camera.getCameraInfo(i, info);
	    if(Camera.CameraInfo.CAMERA_FACING_FRONT == info.facing){
	        return true;
	    }
	}
	return false;
}
 
Example 15
Source File: AndroidCameraAgentImpl.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
public static AndroidCameraDeviceInfo create() {
    int numberOfCameras;
    Camera.CameraInfo[] cameraInfos;
    try {
        numberOfCameras = Camera.getNumberOfCameras();
        cameraInfos = new Camera.CameraInfo[numberOfCameras];
        for (int i = 0; i < numberOfCameras; i++) {
            cameraInfos[i] = new Camera.CameraInfo();
            Camera.getCameraInfo(i, cameraInfos[i]);
        }
    } catch (RuntimeException ex) {
        Log.e(TAG, "Exception while creating CameraDeviceInfo", ex);
        return null;
    }

    int firstFront = NO_DEVICE;
    int firstBack = NO_DEVICE;
    // Get the first (smallest) back and first front camera id.
    for (int i = numberOfCameras - 1; i >= 0; i--) {
        if (cameraInfos[i].facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            firstBack = i;
        } else {
            if (cameraInfos[i].facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                firstFront = i;
            }
        }
    }

    return new AndroidCameraDeviceInfo(cameraInfos, numberOfCameras, firstBack, firstFront);
}
 
Example 16
Source File: CameraView.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public boolean isMultiCamera() {
  return Camera.getNumberOfCameras() > 1;
}
 
Example 17
Source File: OpenCameraInterface.java    From CodeScaner with MIT License 4 votes vote down vote up
/**
 * Opens the requested camera with {@link Camera#open(int)}, if one exists.
 *
 * @param cameraId camera ID of the camera to use. A negative value
 *  or {@link #NO_REQUESTED_CAMERA} means "no preference", in which case a rear-facing
 *  camera is returned if possible or else any camera
 * @return handle to {@link OpenCamera} that was opened
 */
public static OpenCamera open(int cameraId) {

  int numCameras = Camera.getNumberOfCameras();
  if (numCameras == 0) {
    Log.w(TAG, "No cameras!");
    return null;
  }

  boolean explicitRequest = cameraId >= 0;

  Camera.CameraInfo selectedCameraInfo = null;
  int index;
  if (explicitRequest) {
    index = cameraId;
    selectedCameraInfo = new Camera.CameraInfo();
    Camera.getCameraInfo(index, selectedCameraInfo);
  } else {
    index = 0;
    while (index < numCameras) {
      Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
      Camera.getCameraInfo(index, cameraInfo);
      CameraFacing reportedFacing = CameraFacing.values()[cameraInfo.facing];
      if (reportedFacing == CameraFacing.BACK) {
        selectedCameraInfo = cameraInfo;
        break;
      }
      index++;
    }
  }

  Camera camera;
  if (index < numCameras) {
    Log.i(TAG, "Opening camera #" + index);
    camera = Camera.open(index);
  } else {
    if (explicitRequest) {
      Log.w(TAG, "Requested camera does not exist: " + cameraId);
      camera = null;
    } else {
      Log.i(TAG, "No camera facing " + CameraFacing.BACK + "; returning camera #0");
      camera = Camera.open(0);
      selectedCameraInfo = new Camera.CameraInfo();
      Camera.getCameraInfo(0, selectedCameraInfo);
    }
  }

  if (camera == null) {
    return null;
  }
  return new OpenCamera(index,
                        camera,
                        CameraFacing.values()[selectedCameraInfo.facing],
                        selectedCameraInfo.orientation);
}
 
Example 18
Source File: PjCameraInfo.java    From pjsip-android with Apache License 2.0 4 votes vote down vote up
public static int GetCameraCount()
   {
return Camera.getNumberOfCameras();
   }
 
Example 19
Source File: OpenCameraInterface.java    From ShareBox with Apache License 2.0 4 votes vote down vote up
/**
 * Opens the requested camera with {@link Camera#open(int)}, if one exists.
 *
 * @param cameraId camera ID of the camera to use. A negative value
 *                 or {@link #NO_REQUESTED_CAMERA} means "no preference", in which case a rear-facing
 *                 camera is returned if possible or else any camera
 * @return handle to {@link OpenCamera} that was opened
 */
public static OpenCamera open(int cameraId) {

    int numCameras = Camera.getNumberOfCameras();
    if (numCameras == 0) {
        Log.w(TAG, "No cameras!");
        return null;
    }

    boolean explicitRequest = cameraId >= 0;

    Camera.CameraInfo selectedCameraInfo = null;
    int index;
    if (explicitRequest) {
        index = cameraId;
        selectedCameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(index, selectedCameraInfo);
    } else {
        index = 0;
        while (index < numCameras) {
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(index, cameraInfo);
            CameraFacing reportedFacing = CameraFacing.values()[cameraInfo.facing];
            if (reportedFacing == CameraFacing.BACK) {
                selectedCameraInfo = cameraInfo;
                break;
            }
            index++;
        }
    }

    Camera camera;
    if (index < numCameras) {
        Log.i(TAG, "Opening camera #" + index);
        camera = Camera.open(index);
    } else {
        if (explicitRequest) {
            Log.w(TAG, "Requested camera does not exist: " + cameraId);
            camera = null;
        } else {
            Log.i(TAG, "No camera facing " + CameraFacing.BACK + "; returning camera #0");
            camera = Camera.open(0);
            selectedCameraInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(0, selectedCameraInfo);
        }
    }

    if (camera == null) {
        return null;
    }
    return new OpenCamera(index,
            camera,
            CameraFacing.values()[selectedCameraInfo.facing],
            selectedCameraInfo.orientation);
}
 
Example 20
Source File: FaceAnalyser.java    From UserAwareVideoView with Apache License 2.0 2 votes vote down vote up
/**
 * Check if the device has front camera or not?
 *
 * @return true if the device has front camera.
 */
private boolean isFrontCameraAvailable() {
    int numCameras = Camera.getNumberOfCameras();
    return numCameras > 0 && mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
}