Example usage for android.hardware Camera getCameraInfo

List of usage examples for android.hardware Camera getCameraInfo

Introduction

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

Prototype

public static void getCameraInfo(int cameraId, CameraInfo cameraInfo) 

Source Link

Document

Returns the information about a particular camera.

Usage

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);
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;//  ww  w.  j a  v  a 2 s  . com
            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);
        if (b) {// w  w w  . jav a  2 s.c o m
            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.almalence.util.Util.java

@TargetApi(9)
public static int getDisplayOrientation(int degrees, int cameraId) {
    // See android.hardware.Camera.setDisplayOrientation for
    // documentation.
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int result;//from   www .  j  a v a 2 s.  c  o m
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    return result;
}

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 {// w  ww  .j  a  va  2  s.  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.jasompeter.openalpr.CameraActivity.java

public Camera.CameraInfo getCurrentCameraInfo() {
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    Camera.getCameraInfo(mCameraId, cameraInfo);
    return cameraInfo;
}

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
 *//*from ww  w .ja  v  a  2s.  com*/
@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);
        if (ci.facing == CameraInfo.CAMERA_FACING_FRONT)
            return i;
    }/*  w  w w  .j a  va 2  s. c  om*/
    return -1; // No front-facing camera found
}

From source file:org.artoolkit.ar.base.camera.CaptureCameraPreview.java

@SuppressWarnings("deprecation") // setPreviewFrameRate, getPreviewFrameRate
@Override/*from  w  w w. j  av a 2s  .  c om*/
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    Log.i(TAG, "surfaceChanged(): called");
    if (camera == null) {
        // Camera wasn't opened successfully?
        Log.e(TAG, "surfaceChanged(): No camera in surfaceChanged");
        return;
    }

    Log.i(TAG, "surfaceChanged(): Surfaced changed, setting up camera and starting preview");
    String camResolution = PreferenceManager.getDefaultSharedPreferences(getContext()).getString(
            "pref_cameraResolution", getResources().getString(R.string.pref_defaultValue_cameraResolution));
    String[] dims = camResolution.split("x", 2);
    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewSize(Integer.parseInt(dims[0]), Integer.parseInt(dims[1]));
    parameters.setPreviewFrameRate(30);
    camera.setParameters(parameters);

    parameters = camera.getParameters();
    captureWidth = parameters.getPreviewSize().width;
    captureHeight = parameters.getPreviewSize().height;
    captureRate = parameters.getPreviewFrameRate();
    int pixelformat = parameters.getPreviewFormat(); // android.graphics.imageformat
    PixelFormat pixelinfo = new PixelFormat();
    PixelFormat.getPixelFormatInfo(pixelformat, pixelinfo);
    int cameraIndex = 0;
    boolean cameraIsFrontFacing = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraIndex = Integer.parseInt(
                PreferenceManager.getDefaultSharedPreferences(getContext()).getString("pref_cameraIndex", "0"));
        Camera.getCameraInfo(cameraIndex, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
            cameraIsFrontFacing = true;
    }

    int bufSize = captureWidth * captureHeight * pixelinfo.bitsPerPixel / 8; // For the default NV21 format, bitsPerPixel = 12.
    Log.i(TAG, "surfaceChanged(): Camera buffers will be " + captureWidth + "x" + captureHeight + "@"
            + pixelinfo.bitsPerPixel + "bpp, " + bufSize + "bytes.");
    cameraWrapper = new CameraWrapper(camera);
    cameraWrapper.configureCallback(this, true, 10, bufSize); // For the default NV21 format, bitsPerPixel = 12.

    camera.startPreview();

    if (listener != null)
        listener.cameraPreviewStarted(captureWidth, captureHeight, captureRate, cameraIndex,
                cameraIsFrontFacing);
}

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

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

    final LaunchActivity activity = (LaunchActivity) getActivity();
    SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(activity.getApplicationContext());

    /*//  w w w  .j av a 2 s  . co m
     * Reset frames.
     */
    String numPhotosPref = preferences.getString(getString(R.string.pref__number_of_photos_key),
            getString(R.string.pref__number_of_photos_default));
    mFramesTotal = Integer.parseInt(numPhotosPref);
    mFrameIndex = 0;
    mFramesData = new byte[mFramesTotal][];

    /*
     * Initialize and set key event handlers.
     */
    mBackPressedHandler = new LaunchActivity.BackPressedHandler() {

        @Override
        public boolean onBackPressed() {
            // If capture sequence is running, exit capture sequence on back pressed event.
            if (mIsCaptureSequenceRunning) {
                activity.replaceFragment(CaptureFragment.newInstance(mUseFrontFacing), false, true);
            }

            return mIsCaptureSequenceRunning;
        }
    };
    activity.setBackPressedHandler(mBackPressedHandler);

    mKeyEventHandler = new LaunchActivity.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.
     */
    mPreferencesButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent preferencesIntent = new Intent(activity, MyPreferenceActivity.class);
            startActivity(preferencesIntent);
        }
    });

    // Show switch button only if more than one camera is available.
    if (mNumCameras > 1) {
        mSwitchButton.setVisibility(View.VISIBLE);
        mSwitchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Switch camera.
                boolean useFrontFacing = false;
                CameraInfo cameraInfo = new CameraInfo();
                if (mCameraId != INVALID_CAMERA_ID) {
                    Camera.getCameraInfo(mCameraId, cameraInfo);
                    if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
                        useFrontFacing = true;
                    }

                    // Relaunch fragment with new camera.
                    activity.replaceFragment(CaptureFragment.newInstance(useFrontFacing), false, true);
                }
            }
        });
    }

    // Get trigger mode preference.
    String triggerPref = preferences.getString(getString(R.string.pref__trigger_key),
            getString(R.string.pref__trigger_default));
    if (triggerPref.equals(getString(R.string.pref__trigger_countdown))) {
        mTriggerMode = TRIGGER_MODE_COUNTDOWN;
    } else if (triggerPref.equals(getString(R.string.pref__trigger_burst))) {
        mTriggerMode = TRIGGER_MODE_BURST;
    } else {
        mTriggerMode = TRIGGER_MODE_MANUAL;
    }

    // Configure title and start button text.
    mTitle.setText(String.format(getString(R.string.capture__title_frame), mFrameIndex + 1, mFramesTotal));
    if (mTriggerMode == TRIGGER_MODE_MANUAL) {
        // Update title.
        mStartButton.setText(getString(R.string.capture__start_manual_button_text));
    } else {
        mStartButton.setText(getString(R.string.capture__start_countdown_button_text));
    }

    // Configure start button behaviour.
    mStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onStartButtonPressedImpl();
        }
    });

    mStartButton.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            onStartButtonPressedImpl();
            return true;
        }
    });
}

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

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

    final LaunchActivity activity = (LaunchActivity) getActivity();
    SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(activity.getApplicationContext());

    /*//  w  ww  .ja  v a 2  s. co m
     * Reset frames.
     */
    // force to use 4 photos
    /*
    String numPhotosPref = preferences.getString(getString(R.string.pref__number_of_photos_key),
        getString(R.string.pref__number_of_photos_default));
        */
    mFramesTotal = 4;//Integer.parseInt(numPhotosPref);
    mFrameIndex = 0;
    mFramesData = new byte[mFramesTotal][];

    /*
     * Initialize and set key event handlers.
     */
    mBackPressedHandler = new LaunchActivity.BackPressedHandler() {

        @Override
        public boolean onBackPressed() {
            // If capture sequence is running, exit capture sequence on back pressed event.
            if (mIsCaptureSequenceRunning) {
                activity.replaceFragment(CaptureFragment.newInstance(mUseFrontFacing), false, true);
            }

            return mIsCaptureSequenceRunning;
        }
    };
    activity.setBackPressedHandler(mBackPressedHandler);

    mKeyEventHandler = new LaunchActivity.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.
     */
    mPreferencesButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent preferencesIntent = new Intent(activity, MyPreferenceActivity.class);
            startActivity(preferencesIntent);
        }
    });

    // Show switch button only if more than one camera is available.
    if (mNumCameras > 1) {
        mSwitchButton.setVisibility(View.VISIBLE);
        mSwitchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Switch camera.
                boolean useFrontFacing = false;
                CameraInfo cameraInfo = new CameraInfo();
                if (mCameraId != INVALID_CAMERA_ID) {
                    Camera.getCameraInfo(mCameraId, cameraInfo);
                    if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
                        useFrontFacing = true;
                    }

                    // Relaunch fragment with new camera.
                    activity.replaceFragment(CaptureFragment.newInstance(useFrontFacing), false, true);
                }
            }
        });
    }

    // Get trigger mode preference.
    /*
    String triggerPref = preferences.getString(getString(R.string.pref__trigger_key),
        getString(R.string.pref__trigger_default));
    if (triggerPref.equals(getString(R.string.pref__trigger_countdown))) {
    mTriggerMode = TRIGGER_MODE_COUNTDOWN;
    } else if (triggerPref.equals(getString(R.string.pref__trigger_burst))) {
    mTriggerMode = TRIGGER_MODE_BURST;
    } else {
    mTriggerMode = TRIGGER_MODE_MANUAL;
    }
    */
    // force to use countdown mode
    mTriggerMode = TRIGGER_MODE_COUNTDOWN;

    // Configure title and start button text.
    mTitle.setText(String.format(getString(R.string.capture__title_frame), mFrameIndex + 1, mFramesTotal));
    if (mTriggerMode == TRIGGER_MODE_MANUAL) {
        // Update title.
        mStartButton.setText(getString(R.string.capture__start_manual_button_text));
    } else {
        mStartButton.setText(getString(R.string.capture__start_countdown_button_text));
    }

    // Configure start button behaviour.
    mStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onStartButtonPressedImpl();
        }
    });

    mStartButton.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            onStartButtonPressedImpl();
            return true;
        }
    });
}