Example usage for android.hardware Camera getNumberOfCameras

List of usage examples for android.hardware Camera getNumberOfCameras

Introduction

In this page you can find the example usage for android.hardware Camera getNumberOfCameras.

Prototype

public native static int getNumberOfCameras();

Source Link

Document

Returns the number of physical cameras available on this device.

Usage

From source file:com.example.android.foldinglayout.FoldingLayoutActivity.java

/**
 * Creates a SurfaceTextureListener in order to prepare a TextureView
 * which displays a live, and continuously updated, feed from the Camera.
 *//*from  w  ww.java  2 s.  c  o m*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private TextureView.SurfaceTextureListener createSurfaceTextureListener() {
    if (hasApiLevel(Build.VERSION_CODES.ICE_CREAM_SANDWICH)) {
        return new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
                mCamera = Camera.open();

                if (mCamera == null && Camera.getNumberOfCameras() > 1) {
                    mCamera = mCamera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
                }

                if (mCamera == null) {
                    return;
                }

                try {
                    mCamera.setPreviewTexture(surfaceTexture);
                    mCamera.setDisplayOrientation(90);
                    mCamera.startPreview();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
                // Ignored, Camera does all the work for us
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
                if (mCamera != null) {
                    mCamera.stopPreview();
                    mCamera.release();
                }
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
                // Invoked every time there's a new Camera preview frame
            }
        };
    } else {
        return null;
    }
}

From source file:com.kevalpatel.preventscreenoff.FaceAnalyser.java

/**
 * Check if the device has front camera or not?
 *
 * @return true if the device has front camera.
 *///  w  ww.  j av a  2  s .co m
@SuppressWarnings("deprecation")
private boolean isFrontCameraAvailable() {
    int numCameras = Camera.getNumberOfCameras();
    return numCameras > 0
            && mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
}

From source file:com.google.android.cameraview.Camera1.java

/**
 * This rewrites {@link #mCameraId} and {@link #mCameraInfo}.
 *//*from   w  ww .  j a v  a  2s . co m*/
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;
}

From source file:org.opencv.samples.facedetect.PlayerViewDemoActivity.java

private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(i, info);// w w  w  . j  a v a  2 s  . c  o m
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

From source file:com.pimp.companionforband.fragments.extras.CameraActivity.java

private int findFrontFacingCamera(boolean b) {
    int cameraId = -1;
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);//from   w  ww .  j a  v a  2 s .c o  m
        if (b) {
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                cameraId = i;
                break;
            }
        } else {
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                cameraId = i;
                break;
            }
        }
    }
    return cameraId;
}

From source file:com.ezartech.ezar.videooverlay.ezAR.java

private void init(JSONArray args, final CallbackContext callbackContext) {
    this.callbackContext = callbackContext;

    supportSnapshot = getSnapshotPlugin() != null;

    if (args != null) {
        String rgb = DEFAULT_RGB;
        try {/*from  w  w w.java  2s .  c o m*/
            rgb = args.getString(0);
        } catch (JSONException e) {
            //do nothing; resort to DEFAULT_RGB
        }

        setBackgroundColor(Color.parseColor(rgb));
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webViewView.setBackgroundColor(getBackgroundColor());
            }
        });
    }

    if (!PermissionHelper.hasPermission(this, permissions[0])) {
        PermissionHelper.requestPermission(this, CAMERA_SEC, Manifest.permission.CAMERA);
        return;
    }

    JSONObject jsonObject = new JSONObject();
    try {
        Display display = activity.getWindowManager().getDefaultDisplay();
        DisplayMetrics m = new DisplayMetrics();
        display.getMetrics(m);

        jsonObject.put("displayWidth", m.widthPixels);
        jsonObject.put("displayHeight", m.heightPixels);

        int mNumberOfCameras = Camera.getNumberOfCameras();
        Log.d(TAG, "Cameras:" + mNumberOfCameras);

        // Find the ID of the back-facing ("default") camera
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < mNumberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);

            Parameters parameters;
            Camera open = null;
            try {
                open = Camera.open(i);
                parameters = open.getParameters();
            } finally {
                if (open != null) {
                    open.release();
                }
            }

            Log.d(TAG, "Camera facing:" + cameraInfo.facing);

            CameraDirection type = null;
            for (CameraDirection f : CameraDirection.values()) {
                if (f.getDirection() == cameraInfo.facing) {
                    type = f;
                }
            }

            if (type != null) {
                double zoom = 0;
                double maxZoom = 0;
                if (parameters.isZoomSupported()) {
                    maxZoom = (parameters.getMaxZoom() + 1) / 10.0;
                    zoom = Math.min(parameters.getZoom() / 10.0 + 1, maxZoom);
                }

                JSONObject jsonCamera = new JSONObject();
                jsonCamera.put("id", i);
                jsonCamera.put("position", type.toString());
                jsonCamera.put("zoom", zoom);
                jsonCamera.put("maxZoom", maxZoom);
                jsonObject.put(type.toString(), jsonCamera);
            }
        }
    } catch (JSONException e) {
        Log.e(TAG, "Can't set exception", e);
    }

    callbackContext.success(jsonObject);
}

From source file:com.gelakinetic.selfr.CameraActivity.java

/**
 * A safe way to get an instance of the Camera object. Also sets up picture size & focus type
 *
 * @param cameraType Camera.CameraInfo.CAMERA_FACING_FRONT or
 *                   Camera.CameraInfo.CAMERA_FACING_BACK
 * @return A Camera object if it was created, or null
 *//*  ww  w.  j a  v  a 2s .c o m*/
@Nullable
private static Camera getCameraInstance(int cameraType) {
    Camera camera = null;
    try {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        /* Scan through all the cameras for one of the specified type */
        for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == cameraType) {
                try {
                    /* Open the camera, get default parameters */
                    camera = Camera.open(camIdx);
                    Camera.Parameters parameters = camera.getParameters();

                    /* Set the image to native resolution */
                    List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
                    Camera.Size nativeSize = null;
                    int maxHeight = Integer.MIN_VALUE;
                    for (Camera.Size size : sizes) {
                        if (size.height > maxHeight) {
                            maxHeight = size.height;
                            nativeSize = size;
                        }
                    }
                    if (nativeSize != null) {
                        parameters.setPictureSize(nativeSize.width, nativeSize.height);
                    }

                    /* Set auto-focus, if we can */
                    List<String> focusModes = parameters.getSupportedFocusModes();
                    if (focusModes != null && focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
                        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                    }

                    /* Set the parameters */
                    camera.setParameters(parameters);

                } catch (RuntimeException e) {
                    /* Eat it */
                }
            }
        }
    } catch (Exception e) {
        /* Camera is not available (in use or does not exist) */
    }
    return camera; /* returns null if camera is unavailable */
}

From source file:com.example.android.camera.CameraActivity.java

public static int getFrontCameraId() {
    CameraInfo ci = new CameraInfo();
    for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
        Camera.getCameraInfo(i, ci);//from w w w .ja  v a 2 s. co m
        if (ci.facing == CameraInfo.CAMERA_FACING_FRONT)
            return i;
    }
    return -1; // No front-facing camera found
}

From source file:demo.camera.library.ui.CameraCaptureActivity.java

private void setupCameraFlipper() {
    View flipper = findViewById(R.id.cameraFlipper);
    if (Camera.getNumberOfCameras() == 1) {
        flipper.setVisibility(View.GONE);
    } else {/*from w w  w .ja va 2 s  .  c o  m*/
        flipper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCameraManager.requestOtherCamera();
                onPauseCameraSetup();
                onResumeCameraSetup();
            }
        });
    }
}

From source file:ca.nehil.rter.streamingapp.StreamingActivity.java

private Camera openCamera() {
    Log.d("CameraDebug", "openCamera");
    Camera cameraDevice = Camera.open();
    numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras && cameraDevice == null; i++) {
        Log.d("CameraDebug", "opening camera #" + String.valueOf(i));
        cameraDevice = Camera.open(i);/*ww w . j a  v  a  2s.  com*/
    }
    try {
        if (cameraDevice == null) {
            throw new Exception("No camera device found");
        }
    } catch (Exception e) {
        Log.d("CameraDebug", "Released cam in openCamera, exception occured");
        cameraDevice.release();
        cameraDevice = null;
        Log.e("CameraDebug", e.getMessage());
        e.printStackTrace();
    }
    return cameraDevice;
}