Search in sources :

Example 1 with Camera

use of android.hardware.Camera in project PermissionsDispatcher by hotchemi.

the class CameraPreviewFragment method initCamera.

private void initCamera() {
    mCamera = getCameraInstance(CAMERA_ID);
    Camera.CameraInfo cameraInfo = null;
    if (mCamera != null) {
        // Get camera info only if the camera is available
        cameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(CAMERA_ID, cameraInfo);
    }
    // Get the rotation of the screen to adjust the preview image accordingly.
    final int displayRotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
    if (getView() == null) {
        return;
    }
    FrameLayout preview = (FrameLayout) getView().findViewById(R.id.camera_preview);
    preview.removeAllViews();
    if (mPreview == null) {
        // Create the Preview view and set it as the content of this Activity.
        mPreview = new CameraPreview(getActivity(), mCamera, cameraInfo, displayRotation);
    } else {
        mPreview.setCamera(mCamera, cameraInfo, displayRotation);
    }
    preview.addView(mPreview);
}
Also used : FrameLayout(android.widget.FrameLayout) Camera(android.hardware.Camera)

Example 2 with Camera

use of android.hardware.Camera in project material-camera by afollestad.

the class CameraFragment method openCamera.

@Override
public void openCamera() {
    final Activity activity = getActivity();
    if (null == activity || activity.isFinishing())
        return;
    try {
        final int mBackCameraId = mInterface.getBackCamera() != null ? (Integer) mInterface.getBackCamera() : -1;
        final int mFrontCameraId = mInterface.getFrontCamera() != null ? (Integer) mInterface.getFrontCamera() : -1;
        if (mBackCameraId == -1 || mFrontCameraId == -1) {
            int numberOfCameras = Camera.getNumberOfCameras();
            if (numberOfCameras == 0) {
                throwError(new Exception("No cameras are available on this device."));
                return;
            }
            for (int i = 0; i < numberOfCameras; i++) {
                //noinspection ConstantConditions
                if (mFrontCameraId != -1 && mBackCameraId != -1)
                    break;
                Camera.CameraInfo info = new Camera.CameraInfo();
                Camera.getCameraInfo(i, info);
                if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT && mFrontCameraId == -1) {
                    mInterface.setFrontCamera(i);
                } else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK && mBackCameraId == -1) {
                    mInterface.setBackCamera(i);
                }
            }
        }
        switch(getCurrentCameraPosition()) {
            case CAMERA_POSITION_FRONT:
                setImageRes(mButtonFacing, mInterface.iconRearCamera());
                break;
            case CAMERA_POSITION_BACK:
                setImageRes(mButtonFacing, mInterface.iconFrontCamera());
                break;
            case CAMERA_POSITION_UNKNOWN:
            default:
                if (getArguments().getBoolean(CameraIntentKey.DEFAULT_TO_FRONT_FACING, false)) {
                    // Check front facing first
                    if (mInterface.getFrontCamera() != null && (Integer) mInterface.getFrontCamera() != -1) {
                        setImageRes(mButtonFacing, mInterface.iconRearCamera());
                        mInterface.setCameraPosition(CAMERA_POSITION_FRONT);
                    } else {
                        setImageRes(mButtonFacing, mInterface.iconFrontCamera());
                        if (mInterface.getBackCamera() != null && (Integer) mInterface.getBackCamera() != -1)
                            mInterface.setCameraPosition(CAMERA_POSITION_BACK);
                        else
                            mInterface.setCameraPosition(CAMERA_POSITION_UNKNOWN);
                    }
                } else {
                    // Check back facing first
                    if (mInterface.getBackCamera() != null && (Integer) mInterface.getBackCamera() != -1) {
                        setImageRes(mButtonFacing, mInterface.iconFrontCamera());
                        mInterface.setCameraPosition(CAMERA_POSITION_BACK);
                    } else {
                        setImageRes(mButtonFacing, mInterface.iconRearCamera());
                        if (mInterface.getFrontCamera() != null && (Integer) mInterface.getFrontCamera() != -1)
                            mInterface.setCameraPosition(CAMERA_POSITION_FRONT);
                        else
                            mInterface.setCameraPosition(CAMERA_POSITION_UNKNOWN);
                    }
                }
                break;
        }
        if (mWindowSize == null)
            mWindowSize = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(mWindowSize);
        final int toOpen = getCurrentCameraId();
        mCamera = Camera.open(toOpen == -1 ? 0 : toOpen);
        Camera.Parameters parameters = mCamera.getParameters();
        List<Camera.Size> videoSizes = parameters.getSupportedVideoSizes();
        if (videoSizes == null || videoSizes.size() == 0)
            videoSizes = parameters.getSupportedPreviewSizes();
        mVideoSize = chooseVideoSize((BaseCaptureActivity) activity, videoSizes);
        Camera.Size previewSize = chooseOptimalSize(parameters.getSupportedPreviewSizes(), mWindowSize.x, mWindowSize.y, mVideoSize);
        if (ManufacturerUtil.isSamsungGalaxyS3()) {
            parameters.setPreviewSize(ManufacturerUtil.SAMSUNG_S3_PREVIEW_WIDTH, ManufacturerUtil.SAMSUNG_S3_PREVIEW_HEIGHT);
        } else {
            parameters.setPreviewSize(previewSize.width, previewSize.height);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
                parameters.setRecordingHint(true);
        }
        Camera.Size mStillShotSize = getHighestSupportedStillShotSize(parameters.getSupportedPictureSizes());
        parameters.setPictureSize(mStillShotSize.width, mStillShotSize.height);
        setCameraDisplayOrientation(parameters);
        mCamera.setParameters(parameters);
        // NOTE: onFlashModesLoaded should not be called while modifying camera parameters as
        //       the flash parameters set in setupFlashMode will then be overwritten
        mFlashModes = CameraUtil.getSupportedFlashModes(this.getActivity(), parameters);
        mInterface.setFlashModes(mFlashModes);
        onFlashModesLoaded();
        createPreview();
        mMediaRecorder = new MediaRecorder();
        onCameraOpened();
    } catch (IllegalStateException e) {
        throwError(new Exception("Cannot access the camera.", e));
    } catch (RuntimeException e2) {
        throwError(new Exception("Cannot access the camera, you may need to restart your device.", e2));
    }
}
Also used : Activity(android.app.Activity) Point(android.graphics.Point) Point(android.graphics.Point) Camera(android.hardware.Camera) MediaRecorder(android.media.MediaRecorder)

Example 3 with Camera

use of android.hardware.Camera in project material-camera by afollestad.

the class CameraFragment method takeStillshot.

@Override
public void takeStillshot() {
    Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {

        public void onShutter() {
        //Log.d(TAG, "onShutter'd");
        }
    };
    Camera.PictureCallback rawCallback = new Camera.PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {
        //Log.d(TAG, "onPictureTaken - raw. Raw is null: " + (data == null));
        }
    };
    Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {

        public void onPictureTaken(final byte[] data, Camera camera) {
            //Log.d(TAG, "onPictureTaken - jpeg, size: " + data.length);
            final File outputPic = getOutputPictureFile();
            // lets save the image to disk
            ImageUtil.saveToDiskAsync(data, outputPic, new ICallback() {

                @Override
                public void done(Exception e) {
                    if (e == null) {
                        Log.d("CameraFragment", "Picture saved to disk - jpeg, size: " + data.length);
                        mOutputUri = Uri.fromFile(outputPic).toString();
                        mInterface.onShowStillshot(mOutputUri);
                        //mCamera.startPreview();
                        mButtonStillshot.setEnabled(true);
                    } else {
                        throwError(e);
                    }
                }
            });
        }
    };
    //        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    //            // We could have configurable shutter sound here
    //            mCamera.enableShutterSound(false);
    //        }
    mButtonStillshot.setEnabled(false);
    mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
Also used : ICallback(com.afollestad.materialcamera.ICallback) Camera(android.hardware.Camera) File(java.io.File)

Example 4 with Camera

use of android.hardware.Camera in project android-gpuimage-ndkbuild-sample by mugku.

the class ActivityCamera method takePicture.

private void takePicture() {
    // TODO get a size that is about the size of the screen
    Camera.Parameters params = mCamera.mCameraInstance.getParameters();
    params.setRotation(90);
    mCamera.mCameraInstance.setParameters(params);
    for (Camera.Size size : params.getSupportedPictureSizes()) {
        Log.i("ASDF", "Supported: " + size.width + "x" + size.height);
    }
    mCamera.mCameraInstance.takePicture(null, null, new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, final Camera camera) {
            final File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if (pictureFile == null) {
                Log.d("ASDF", "Error creating media file, check storage permissions");
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d("ASDF", "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d("ASDF", "Error accessing file: " + e.getMessage());
            }
            data = null;
            Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
            // mGPUImage.setImage(bitmap);
            final GLSurfaceView view = (GLSurfaceView) findViewById(R.id.surfaceView);
            view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
            mGPUImage.saveToPictures(bitmap, "GPUImage", System.currentTimeMillis() + ".jpg", new GPUImage.OnPictureSavedListener() {

                @Override
                public void onPictureSaved(final Uri uri) {
                    pictureFile.delete();
                    camera.startPreview();
                    view.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
                }
            });
        }
    });
}
Also used : Parameters(android.hardware.Camera.Parameters) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) GLSurfaceView(android.opengl.GLSurfaceView) Uri(android.net.Uri) Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) Camera(android.hardware.Camera) File(java.io.File)

Example 5 with Camera

use of android.hardware.Camera in project android_frameworks_base by ResurrectionRemix.

the class MediaRecorderTest method testPortraitH263.

@LargeTest
public /*
     * This test case set the camera in portrait mode.
     * Verification: validate the video dimension and the duration.
     */
void testPortraitH263() throws Exception {
    boolean videoRecordedResult = false;
    try {
        mCamera = Camera.open(CAMERA_ID);
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(352, 288);
        parameters.set("orientation", "portrait");
        mCamera.setParameters(parameters);
        mCamera.unlock();
        mRecorder.setCamera(mCamera);
        Thread.sleep(1000);
        int codec = MediaRecorder.VideoEncoder.H263;
        int frameRate = MediaProfileReader.getMaxFrameRateForCodec(codec);
        recordVideo(frameRate, 352, 288, codec, MediaRecorder.OutputFormat.THREE_GPP, MediaNames.RECORDED_PORTRAIT_H263, true);
        mCamera.lock();
        mCamera.release();
        videoRecordedResult = validateVideo(MediaNames.RECORDED_PORTRAIT_H263, 352, 288);
    } catch (Exception e) {
        Log.v(TAG, e.toString());
    }
    assertTrue("PortraitH263", videoRecordedResult);
}
Also used : Camera(android.hardware.Camera) Paint(android.graphics.Paint) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Aggregations

Camera (android.hardware.Camera)281 Point (android.graphics.Point)50 IOException (java.io.IOException)39 WindowManager (android.view.WindowManager)32 Display (android.view.Display)30 Size (android.util.Size)27 Rect (android.graphics.Rect)25 CameraCharacteristics (android.hardware.camera2.CameraCharacteristics)25 ArrayList (java.util.ArrayList)24 Parameters (android.hardware.Camera.Parameters)21 SharedPreferences (android.content.SharedPreferences)20 CaptureRequest (android.hardware.camera2.CaptureRequest)15 SuppressLint (android.annotation.SuppressLint)14 Paint (android.graphics.Paint)14 OpenCamera (com.google.zxing.client.android.camera.open.OpenCamera)12 Bitmap (android.graphics.Bitmap)11 CameraMetadataNative (android.hardware.camera2.impl.CameraMetadataNative)10 ZoomData (android.hardware.camera2.legacy.ParameterUtils.ZoomData)10 Size (android.hardware.Camera.Size)9 MediaRecorder (android.media.MediaRecorder)7