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.cellbots.logger.LauncherActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ///< Bind the MetaWear service when the activity is created
    setContentView(R.layout.main);//from  w ww .j a  v a  2  s .  c o  m

    useZipCheckbox = (CheckBox) findViewById(R.id.useZip);

    final Activity self = this;
    Button launchVideoFrontButton;
    launchVideoFrontButton = (Button) findViewById(R.id.launchVideoFront);
    launchVideoFrontButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            launchLoggingActivity(LoggerActivity.MODE_VIDEO_FRONT, useZipCheckbox.isChecked());
        }
    });
    Button launchVideoBackButton = (Button) findViewById(R.id.launchVideoBack);
    launchVideoBackButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            launchLoggingActivity(LoggerActivity.MODE_VIDEO_BACK, useZipCheckbox.isChecked());
        }
    });

    Button connectMW = (Button) findViewById(R.id.mwconnect);
    connectMW.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            new MWScannerFragment().show(getFragmentManager(), "metawear_scanner_fragment");
        }
    });

    final EditText pictureDelayEditText = (EditText) findViewById(R.id.pictureDelay);
    Button launchPictureButton = (Button) findViewById(R.id.launchPicture);
    launchPictureButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(self, LoggerActivity.class);
            i.putExtra(LoggerActivity.EXTRA_MODE, LoggerActivity.MODE_PICTURES);
            i.putExtra(LoggerActivity.EXTRA_USE_ZIP, useZipCheckbox.isChecked());
            int delay = 30;
            try {
                delay = Integer.parseInt(pictureDelayEditText.getText().toString());
            } catch (Exception e) {
                Toast.makeText(self, "Error parsing picture delay time. Using default delay of 30 seconds.",
                        Toast.LENGTH_LONG).show();
            }
            i.putExtra(LoggerActivity.EXTRA_PICTURE_DELAY, delay);
            startActivity(i);
            finish();
        }
    });
    // The code we are using for taking video through the front camera
    // relies on APIs added in SDK 9. Don't offer the front video option to
    // users on devices older than that OR to devices who have only one
    // camera. Currently assume that if only one camera is present, it is
    // the back camera.
    if (Build.VERSION.SDK_INT < 9 || Camera.getNumberOfCameras() == 1) {
        launchVideoFrontButton.setVisibility(View.GONE);
    }
}

From source file:uk.ac.horizon.artcodes.camera.CameraView.java

public void setDetector(Detector processor, Experience experience) {
    this.experience = experience;
    this.detector = processor;
    if (detector != null) {
        if (camera == null) {
            createCamera();/*from ww  w  .  j a v a 2  s .c o  m*/
        }
        if (camera != null) {
            cameraIsFocused = !deviceNeedsTapToFocus;
            camera.addCallbackBuffer(detector.createBuffer(info, surfaceWidth, surfaceHeight));
            camera.setPreviewCallbackWithBuffer(new Camera.PreviewCallback() {
                @Override
                public void onPreviewFrame(final byte[] data, final Camera camera) {

                    boolean focusedOnThisFrame = false;

                    if (deviceNeedsTapToFocus || !cameraIsFocused) {
                        synchronized (cameraFocusLock) {
                            while (!cameraIsFocused) {
                                try {
                                    Log.i("FOCUS", "cameraFocusLock.wait(); Thread: "
                                            + Thread.currentThread().getName());
                                    focusedOnThisFrame = true;
                                    cameraFocusLock.wait();
                                } catch (InterruptedException ignored) {
                                }
                            }
                            Log.i("FOCUS", "notified Thread: " + Thread.currentThread().getName());
                        }
                    }

                    if (cameraIsFocused) {
                        Log.i("FOCUS", "cameraIsFocused test: " + cameraIsFocused);
                        if (!focusedOnThisFrame) {
                            detector.setData(data);
                        }
                        camera.addCallbackBuffer(data);
                    }
                }
            });
        }

        if (Camera.getNumberOfCameras() > 1) {
            detector.getSettings().add(new DetectorSetting() {
                @Override
                public void nextValue() {
                    stopCamera();
                    facing = 1 - facing;
                    startCamera();
                }

                @Override
                public int getIcon() {
                    switch (facing) {
                    case Camera.CameraInfo.CAMERA_FACING_BACK:
                        return R.drawable.ic_camera_rear_24dp;
                    case Camera.CameraInfo.CAMERA_FACING_FRONT:
                        return R.drawable.ic_camera_front_24dp;
                    }
                    return 0;
                }

                @Override
                public int getText() {
                    switch (facing) {
                    case Camera.CameraInfo.CAMERA_FACING_BACK:
                        return R.string.camera_rear;
                    case Camera.CameraInfo.CAMERA_FACING_FRONT:
                        return R.string.camera_front;
                    }

                    return 0;
                }
            });
        }
    }
}

From source file:com.groundupworks.partyphotobooth.fragments.CaptureFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    final KioskActivity activity = (KioskActivity) getActivity();
    final Context appContext = activity.getApplicationContext();
    final Bundle args = getArguments();
    final int totalFrames = args.getInt(FRAGMENT_BUNDLE_KEY_TOTAL_FRAMES);
    final int currentFrame = args.getInt(FRAGMENT_BUNDLE_KEY_CURRENT_FRAME);

    /*/*from   w  w w. j  a  v a2  s  .  c  o  m*/
     * Select camera from preference.
     */
    // Get from preference.
    PreferencesHelper preferencesHelper = new PreferencesHelper();
    PhotoBoothMode mode = preferencesHelper.getPhotoBoothMode(appContext);

    int cameraPreference = CameraInfo.CAMERA_FACING_FRONT;
    if (PhotoBoothMode.PHOTOGRAPHER.equals(mode)) {
        cameraPreference = CameraInfo.CAMERA_FACING_BACK;
    }

    // Default to first camera available.
    final int numCameras = Camera.getNumberOfCameras();
    if (numCameras > 0) {
        mCameraId = 0;
    }

    // Select preferred camera.
    CameraInfo cameraInfo = new CameraInfo();
    for (int cameraId = 0; cameraId < numCameras; cameraId++) {
        Camera.getCameraInfo(cameraId, cameraInfo);

        // Break on finding the preferred camera.
        if (cameraInfo.facing == cameraPreference) {
            mCameraId = cameraId;
            break;
        }
    }

    /*
    * Initialize and set key event handlers.
    */
    mKeyEventHandler = new KioskActivity.KeyEventHandler() {
        @Override
        public boolean onKeyEvent(KeyEvent event) {
            final int keyCode = event.getKeyCode();
            if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                mStartButton.dispatchKeyEvent(event);
                return true;
            }
            return false;
        }
    };
    activity.setKeyEventHandler(mKeyEventHandler);

    /*
     * Functionalize views.
     */
    // Configure start button and trigger behaviour.
    switch (mode) {
    case PHOTOGRAPHER:
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initiateCapture();
            }
        });
        mStartButton.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                initiateCapture();
                return true;
            }
        });
        linkStartButton();
        break;
    case AUTOMATIC:
        if (currentFrame > 1) {
            // Auto-trigger when camera is ready and preview has started.
            mPreview.setOnPreviewListener(new CenteredPreview.OnPreviewListener() {
                @Override
                public void onStarted() {
                    if (isActivityAlive()) {
                        mPreview.post(new Runnable() {
                            @Override
                            public void run() {
                                if (isActivityAlive()) {
                                    initiateCountdownCapture();
                                }
                            }
                        });
                    }
                }

                @Override
                public void onStopped() {
                    // Do nothing.
                }
            });
            break;
        } else {
            // Fall through to self-serve behaviour. Do not break.
        }
    case SELF_SERVE:
    default:
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initiateCountdownCapture();
            }
        });
        mStartButton.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                initiateCountdownCapture();
                return true;
            }
        });
        linkStartButton();
    }

    // Show frame count only if more than one frame is to be captured.
    if (totalFrames > 1) {
        String frameCountText = getString(R.string.capture__frame_count, currentFrame, totalFrames);
        mFrameCount.setTypeface(
                Theme.from(appContext, preferencesHelper.getPhotoBoothTheme(appContext)).getFont());
        mFrameCount.setText(frameCountText);
        mFrameCount.setVisibility(View.VISIBLE);
    }
}

From source file:com.fatelon.partyphotobooth.fragments.CaptureFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    final KioskActivity activity = (KioskActivity) getActivity();
    final Context appContext = activity.getApplicationContext();
    final Bundle args = getArguments();
    final int totalFrames = args.getInt(FRAGMENT_BUNDLE_KEY_TOTAL_FRAMES);
    final int currentFrame = args.getInt(FRAGMENT_BUNDLE_KEY_CURRENT_FRAME);

    /*//  w w w  .j  a  v  a  2  s.c  o m
     * Select camera from preference.
     */
    // Get from preference.
    PreferencesHelper preferencesHelper = new PreferencesHelper();
    mPhotoBoothMode = preferencesHelper.getPhotoBoothMode(appContext);

    int cameraPreference = CameraInfo.CAMERA_FACING_FRONT;
    if (PreferencesHelper.PhotoBoothMode.PHOTOGRAPHER.equals(mPhotoBoothMode)) {
        cameraPreference = CameraInfo.CAMERA_FACING_BACK;
    }

    // Default to first camera available.
    final int numCameras = Camera.getNumberOfCameras();
    if (numCameras > 0) {
        mCameraId = 0;
    }

    // Select preferred camera.
    CameraInfo cameraInfo = new CameraInfo();
    for (int cameraId = 0; cameraId < numCameras; cameraId++) {
        Camera.getCameraInfo(cameraId, cameraInfo);

        // Break on finding the preferred camera.
        if (cameraInfo.facing == cameraPreference) {
            mCameraId = cameraId;
            break;
        }
    }

    /*
    * Initialize and set key event handlers.
    */
    mKeyEventHandler = new KioskActivity.KeyEventHandler() {
        @Override
        public boolean onKeyEvent(KeyEvent event) {
            final int keyCode = event.getKeyCode();
            if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                mStartButton.dispatchKeyEvent(event);
                return true;
            }
            return false;
        }
    };
    activity.setKeyEventHandler(mKeyEventHandler);

    /*
     * Functionalize views.
     */
    // Configure start button and trigger behaviour.
    switch (mPhotoBoothMode) {
    case PHOTOGRAPHER:
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initiateCapture();
            }
        });
        mStartButton.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                initiateCapture();
                return true;
            }
        });
        linkStartButton();
        break;
    case AUTOMATIC:
        mStartButtonLong.setVisibility(View.VISIBLE);
        mStartButton.setVisibility(View.GONE);
        mStartButton = mStartButtonLong;
        if (currentFrame > 1) {
            // Auto-trigger when camera is ready and preview has started.
            mPreview.setOnPreviewListener(new CenteredPreview.OnPreviewListener() {
                @Override
                public void onStarted() {
                    if (isActivityAlive()) {
                        mPreview.post(new Runnable() {
                            @Override
                            public void run() {
                                if (isActivityAlive()) {
                                    initiateCountdownCapture();
                                }
                            }
                        });
                    }
                }

                @Override
                public void onStopped() {
                    // Do nothing.
                }
            });
            break;
        } else {
            // Fall through to self-serve behaviour. Do not break.
        }
    case SELF_SERVE:
    default:
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initiateCountdownCapture();
            }
        });
        mStartButton.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                initiateCountdownCapture();
                return true;
            }
        });
        linkStartButton();
    }

    // Show frame count only if more than one frame is to be captured.
    if (totalFrames > 1) {
        String frameCountText = getString(R.string.capture__frame_count, currentFrame, totalFrames);
        mFrameCount.setTypeface(
                Theme.from(appContext, preferencesHelper.getPhotoBoothTheme(appContext)).getFont());
        mFrameCount.setText(frameCountText);
        mFrameCount.setVisibility(View.VISIBLE);
    }
}

From source file:com.grayraven.barcoder.CameraActivityFragement.java

private boolean safeCameraOpen() {
    boolean qOpened = false;

    try {//from   w w w  . ja  va2  s  . c om
        stopPreviewAndFreeCamera();

        //get index of front camera
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        int count = Camera.getNumberOfCameras();
        for (int i = 0; i < count; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                mCamera = Camera.open(i);
                mCameraId = i;
                break;
            }
        }
        qOpened = (mCamera != null);
    } catch (Exception e) {
        Log.e(getString(R.string.app_name), "failed to open Camera");
        e.printStackTrace();
    }

    return qOpened;
}

From source file:com.microsoft.mimickeralarm.utilities.GeneralUtilities.java

@SuppressWarnings("deprecation")
private static boolean hasDeviceCameraWithDirection(int cameraDirection) {
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    for (int i = 0; i < Camera.getNumberOfCameras(); i++) {

        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == cameraDirection) {
            return true;
        }//from   www  .  j  a v a2s.  c o m
    }
    return false;
}

From source file:uk.ac.horizon.artcodes.camera.CameraView.java

private void createCamera() {
    if (ContextCompat.checkSelfPermission(getContext(),
            Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
        for (int cameraId = 0; cameraId < Camera.getNumberOfCameras(); cameraId++) {
            try {
                Camera.CameraInfo info = new Camera.CameraInfo();
                Camera.getCameraInfo(cameraId, info);

                if (info.facing == facing) {
                    openCamera(cameraId);
                    return;
                }/*from  w  w  w . ja  va  2  s.  com*/
            } catch (RuntimeException e) {
                Log.e("Scanner", "Failed to open scanner " + cameraId + ": " + e.getLocalizedMessage(), e);
            }
        }

        for (int cameraId = 0; cameraId < Camera.getNumberOfCameras(); cameraId++) {
            try {
                openCamera(cameraId);
                return;
            } catch (RuntimeException e) {
                Log.e("Scanner", "Failed to open scanner " + cameraId + ": " + e.getLocalizedMessage(), e);
            }
        }
    }
}

From source file:com.arthurtimberly.fragments.CaptureFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /*/*from   w w w. j a v a 2s.c om*/
     * Get params.
     */
    Bundle args = getArguments();
    mUseFrontFacing = args.getBoolean(FRAGMENT_BUNDLE_KEY_CAMERA);

    /*
     * Default to first camera available.
     */
    mNumCameras = Camera.getNumberOfCameras();
    if (mNumCameras > 0) {
        mCameraId = 0;
    }

    /*
     * Try to select camera based on preference.
     */
    int cameraPreference = CameraInfo.CAMERA_FACING_BACK;
    if (mUseFrontFacing) {
        cameraPreference = CameraInfo.CAMERA_FACING_FRONT;
    }

    CameraInfo cameraInfo = new CameraInfo();
    for (int cameraId = 0; cameraId < mNumCameras; cameraId++) {
        Camera.getCameraInfo(cameraId, cameraInfo);

        // Set flag to indicate whether the camera image is reflected.
        mIsReflected = isCameraImageReflected(cameraInfo);

        // Break on finding the preferred camera.
        if (cameraInfo.facing == cameraPreference) {
            mCameraId = cameraId;
            break;
        }
    }
}

From source file:ru.jango.j0widget.camera.fragment.AbstractCameraFragment.java

private int getBackwardCameraId() {
    for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
        final Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);//from ww  w.  j  a  va2 s .c o m
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
            return i;
    }

    return 0;
}

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

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