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:Main.java

/**
 * Return the back facing camera id// w  ww. j  a  v  a  2  s . c om
 * @return id of the camera
 */
public static int getCameraInformationId() {
    int cameraId = -1;
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

From source file:com.actionbarsherlock.sample.hcgallery.CameraFragment.java

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

    // Create a RelativeLayout container that will hold a SurfaceView,
    // and set it as the content of our activity.
    mPreview = new Preview(this.getActivity());

    // Find the total number of cameras available
    mNumberOfCameras = Camera.getNumberOfCameras();

    // Find the ID of the default camera
    CameraInfo cameraInfo = new CameraInfo();
    for (int i = 0; i < mNumberOfCameras; i++) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
            mDefaultCameraId = i;//from w  w  w  .  j  a v a 2 s  .c o m
        }
    }
    setHasOptionsMenu(mNumberOfCameras > 1);
}

From source file:com.fallahpoor.infocenter.fragments.CameraFragment.java

private void populateListView(TextView msgTextView) {

    if (Camera.getNumberOfCameras() > 0) {
        mGetCameraParamsTask = new GetCameraParamsTask();
        mGetCameraParamsTask.execute();//from www .  ja va2 s . c om
    } else {
        mListView.setEmptyView(msgTextView);
        mListView = null;
    }

}

From source file:com.ezartech.ezar.flashlight.Flashlight.java

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

    if (!PermissionHelper.hasPermission(this, permissions[0])) {
        PermissionHelper.requestPermission(this, CAMERA_SEC, Manifest.permission.CAMERA);
        return;// w  ww .java  2 s  .  com
    }

    JSONObject jsonResult = new JSONObject();

    try {
        jsonResult.put("back", false);

        int mNumberOfCameras = Camera.getNumberOfCameras();

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

            if (cameraInfo.facing != CameraInfo.CAMERA_FACING_BACK) {
                continue;
            }

            Parameters parameters;
            Camera camera = null;
            Camera releasableCamera = null;
            try {

                try {
                    if (id != voCameraId) {
                        camera = Camera.open(id);
                    } else {
                        camera = voCamera;
                    }

                } catch (RuntimeException re) {
                    System.out.println("Failed to open CAMERA: " + id);
                    continue;
                }

                parameters = camera.getParameters();

                List<String> torchModes = parameters.getSupportedFlashModes();
                boolean hasLight = torchModes != null && torchModes.contains(Parameters.FLASH_MODE_TORCH);

                if (hasLight) {
                    String key = "back";
                    cameraId = id;
                    ;
                    jsonResult.put(key, true);
                }

                //determine if camera should be released
                if (id != voCameraId) {
                    releasableCamera = camera;
                }

            } finally {
                if (releasableCamera != null) {
                    releasableCamera.release();
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Can't set exception", e);
        callbackContext.error("Unable to access Camera for light information.");
        return;
    }

    callbackContext.success(jsonResult);
}

From source file:com.fallahpoor.infocenter.fragments.CameraFragment.java

private ArrayList<ListItem> getListItems() {

    ArrayList<ListItem> items = new ArrayList<>();

    switch (Camera.getNumberOfCameras()) {
    case 1:/*from   w w w. j a v  a2  s  .  c o  m*/
        items.addAll(getCameraParams(CameraInfo.CAMERA_FACING_BACK));
        break;
    case 2:
        items.add(new HeaderListItem(getString(R.string.cam_item_back_camera)));
        items.addAll(getCameraParams(CameraInfo.CAMERA_FACING_BACK));
        items.add(new HeaderListItem(getString(R.string.cam_item_front_camera)));
        items.addAll(getCameraParams(CameraInfo.CAMERA_FACING_FRONT));
    }

    return items;

}

From source file:com.czh.testmpeg.videorecord.CameraActivity.java

/**
 * ??,-1//from  w  w  w  .  j  ava2s.c o m
 *
 * @return cameraId
 */
private int findFrontFacingCamera() {
    int cameraId = -1;
    //??
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            cameraFront = true;
            break;
        }
    }
    return cameraId;
}

From source file:com.czh.testmpeg.videorecord.CameraActivity.java

/**
 * ??,-1/*from  w w  w  .ja  va2 s .  c om*/
 *
 * @return cameraId
 */
private int findBackFacingCamera() {
    int cameraId = -1;
    //??
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            cameraId = i;
            cameraFront = false;
            break;
        }
    }
    return cameraId;
}

From source file:com.TaxiDriver.jy.CameraPreview.java

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

    // Hide the window title.
    // requestWindowFeature(Window.FEATURE_NO_TITLE);
    // getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.camera);//from  www . jav  a  2  s .com

    // Create a RelativeLayout container that will hold a SurfaceView,
    // and set it as the content of our activity.
    mPreview = new Preview(this);
    ((FrameLayout) findViewById(R.id.preview)).addView(mPreview);
    buttonRetry = (Button) findViewById(R.id.buttonRetry);
    buttonSend = (Button) findViewById(R.id.buttonSend);

    buttonClick = (Button) findViewById(R.id.buttonClick);
    buttonClick.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            mCamera.takePicture(null, null, jpegCallback);

        }
    });

    // Find the total number of cameras available
    numberOfCameras = Camera.getNumberOfCameras();

    // Find the ID of the default camera
    CameraInfo cameraInfo = new CameraInfo();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
            defaultCameraId = i;
        }
    }
}

From source file:com.jasompeter.openalpr.CameraActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_camera);

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface_view);
    mTorchButton = (ImageButton) findViewById(R.id.torch_button);
    mCapturingText = (TextView) findViewById(R.id.capturing_text);
    mTouchToCaptureText = (TextView) findViewById(R.id.touch_to_capture);
    mSurfaceHolder = surfaceView.getHolder();
    mSurfaceHolder.addCallback(this);
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    // we should not open camera on UI thread
    mCameraOpenTask = new AsyncTask<Void, Void, Void>() {
        @Override/*  w  ww  .  j  a v  a 2 s  . co m*/
        protected Void doInBackground(Void[] params) {

            if (!mCameraIsOpen) {
                releaseCameraAndPreview();
                try {

                    int cameraCount = Camera.getNumberOfCameras();
                    Log.d(TAG, "We have " + cameraCount + " cameras.");
                    for (int i = 0; i < cameraCount; i++) {
                        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
                        Camera.getCameraInfo(i, cameraInfo);
                        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                            mCameraId = i;
                            break;
                        }
                    }

                    mCamera = Camera.open(mCameraId);
                    mCameraIsOpen = true;
                } catch (Exception e) {
                    mCameraIsOpen = false;
                    Log.d(TAG, "Failed to open camera. Camera is probably in use.");
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void o) {
            onCameraOpened(mCamera != null);
        }
    };

    // orientation manager handles orientation changes
    // because our activity is set to force landscape mode
    // and we may want rotate action buttons or do something with view
    mOrientationManager = new OrientationManager(this, SensorManager.SENSOR_DELAY_NORMAL, this);

    invalidateTorchButton();
    mTorchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            toggleTorch();
        }
    });

    initializeAlpr();

    surfaceView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mEnableRecognition = true;
                mCapturingText.setVisibility(View.VISIBLE);
                mTouchToCaptureText.setVisibility(View.GONE);
                return true; // if you want to handle the touch event
            case MotionEvent.ACTION_UP:
                mEnableRecognition = false;
                mCapturingText.setVisibility(View.GONE);
                mTouchToCaptureText.setVisibility(View.VISIBLE);
                return true; // if you want to handle the touch event
            }
            return false;
        }
    });

}

From source file:com.google.firebase.samples.apps.mlkit.java.LivePreviewActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate");

    setContentView(R.layout.activity_live_preview);

    preview = (CameraSourcePreview) findViewById(R.id.firePreview);
    if (preview == null) {
        Log.d(TAG, "Preview is null");
    }//from  w w w.j a  v  a 2 s . c  o  m
    graphicOverlay = (GraphicOverlay) findViewById(R.id.fireFaceOverlay);
    if (graphicOverlay == null) {
        Log.d(TAG, "graphicOverlay is null");
    }

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    List<String> options = new ArrayList<>();
    options.add(FACE_CONTOUR);
    options.add(FACE_DETECTION);
    options.add(TEXT_DETECTION);
    options.add(BARCODE_DETECTION);
    options.add(IMAGE_LABEL_DETECTION);
    options.add(CLASSIFICATION_QUANT);
    options.add(CLASSIFICATION_FLOAT);
    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.spinner_style, options);
    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);
    spinner.setOnItemSelectedListener(this);

    ToggleButton facingSwitch = (ToggleButton) findViewById(R.id.facingSwitch);
    facingSwitch.setOnCheckedChangeListener(this);
    // Hide the toggle button if there is only 1 camera
    if (Camera.getNumberOfCameras() == 1) {
        facingSwitch.setVisibility(View.GONE);
    }

    if (allPermissionsGranted()) {
        createCameraSource(selectedModel);
    } else {
        getRuntimePermissions();
    }
}