Example usage for android.hardware Camera getParameters

List of usage examples for android.hardware Camera getParameters

Introduction

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

Prototype

public Parameters getParameters() 

Source Link

Document

Returns the current settings for this Camera service.

Usage

From source file:com.googlecode.android_scripting.facade.CameraFacade.java

@Rpc(description = "Take a picture and save it to the specified path.", returns = "A map of Booleans autoFocus and takePicture where True "
        + "indicates success. cameraId also included.")
public Bundle cameraCapturePicture(@RpcParameter(name = "targetPath") final String targetPath,
        @RpcParameter(name = "useAutoFocus") @RpcDefault("true") Boolean useAutoFocus,
        @RpcParameter(name = "cameraId", description = "Id of camera to use. SDK 9") @RpcDefault("0") Integer cameraId)
        throws Exception {
    final BooleanResult autoFocusResult = new BooleanResult();
    final BooleanResult takePictureResult = new BooleanResult();
    Camera camera = openCamera(cameraId);
    if (camera == null) {
        String msg = String.format("can't initialize camera id %d, try to use another id", cameraId);
        Log.e(msg);/*from   w  ww  . jav  a 2  s .c  o m*/

        Bundle result = new Bundle();
        result.putInt("cameraId", cameraId);
        result.putBoolean("autoFocus", false);
        result.putBoolean("takePicture", false);
        result.putString("reason", msg + ", see logcat for details");
        return result;
    }
    Parameters prm = camera.getParameters();
    camera.setParameters(prm);

    try {
        Method method = camera.getClass().getMethod("setDisplayOrientation", int.class);
        method.invoke(camera, 90);
    } catch (Exception e) {
        Log.e(e);
    }

    try {
        FutureActivityTask<SurfaceHolder> previewTask = setPreviewDisplay(camera);
        camera.startPreview();
        if (useAutoFocus) {
            autoFocus(autoFocusResult, camera);
        }
        takePicture(new File(targetPath), takePictureResult, camera);
        previewTask.finish();
    } catch (Exception e) {
        Log.e(e);
    } finally {
        camera.release();
    }

    Bundle result = new Bundle();
    result.putBoolean("autoFocus", autoFocusResult.mmResult);
    result.putBoolean("takePicture", takePictureResult.mmResult);
    result.putInt("cameraId", cameraId);
    return result;
}

From source file:com.nekomeshi312.whiteboardcorrection.CameraViewFragment.java

private void setPreviewCallback() {
    if (!mCameraSetting.isCameraOpen())
        return;//  ww w.jav  a  2 s. c om

    final Camera camera = mCameraSetting.getCamera();
    //?? bit/pixel
    PixelFormat pixelinfo = new PixelFormat();
    int pixelformat = camera.getParameters().getPreviewFormat();
    PixelFormat.getPixelFormatInfo(pixelformat, pixelinfo);
    //???
    Camera.Parameters parameters = camera.getParameters();
    Size sz = parameters.getPreviewSize();
    //??????
    int bufSize = sz.width * sz.height * pixelinfo.bitsPerPixel / 8;
    mBuffer = new byte[bufSize];
    camera.addCallbackBuffer(mBuffer);
    camera.setPreviewCallbackWithBuffer(this);
}

From source file:org.deviceconnect.android.deviceplugin.host.camera.CameraOverlay.java

private void setRequestedPictureSize(final Camera camera) {
    HostDeviceRecorder.PictureSize currentSize = mPictureSize;
    if (camera != null && currentSize != null) {
        Camera.Parameters params = camera.getParameters();
        params.setPictureSize(currentSize.getWidth(), currentSize.getHeight());
        camera.setParameters(params);/*from   w  w  w.  j  av  a  2 s . c  o  m*/
    }
}

From source file:org.deviceconnect.android.deviceplugin.host.camera.CameraOverlay.java

private void setRequestedPreviewSize(final Camera camera) {
    HostDeviceRecorder.PictureSize currentSize = mPreviewSize;
    if (camera != null && currentSize != null) {
        Camera.Parameters params = camera.getParameters();
        params.setPreviewSize(currentSize.getWidth(), currentSize.getHeight());
        camera.setParameters(params);/*from   ww  w  .  j av a 2s  .  c  o m*/
    }
}

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

@Override
protected void onResume() {
    super.onResume();

    Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {

        @Override/*from  www .j  a v a  2s  .c  o  m*/
        public void onPreviewFrame(byte[] data, Camera camera) {

            if (notRequesting && mPreview.faces.size() >= 1 && imageFormat == ImageFormat.NV21) {

                // Block Request.
                notRequesting = false;

                try {
                    Camera.Parameters parameters = camera.getParameters();
                    Size size = parameters.getPreviewSize();

                    textServerView.setText("Preparing Image to send");
                    YuvImage previewImg = new YuvImage(data, parameters.getPreviewFormat(), size.width,
                            size.height, null);
                    pWidth = previewImg.getWidth();
                    pHeight = previewImg.getHeight();

                    Log.d("face View", "Width: " + pWidth + " x Height: " + pHeight);
                    prepareMatrix(matrix, 0, pWidth, pHeight);
                    List<Rect> foundFaces = getFaces();

                    for (Rect cRect : foundFaces) {
                        // Cropping
                        ByteArrayOutputStream bao = new ByteArrayOutputStream();
                        previewImg.compressToJpeg(cRect, 100, bao);
                        byte[] mydata = bao.toByteArray();

                        // Resizing
                        ByteArrayOutputStream sbao = new ByteArrayOutputStream();
                        Bitmap bm = BitmapFactory.decodeByteArray(mydata, 0, mydata.length);
                        Bitmap sbm = Bitmap.createScaledBitmap(bm, 100, 100, true);
                        bm.recycle();
                        sbm.compress(Bitmap.CompressFormat.JPEG, 100, sbao);
                        byte[] mysdata = sbao.toByteArray();

                        RequestParams params = new RequestParams();
                        params.put("upload", new ByteArrayInputStream(mysdata), "tmp.jpg");

                        textServerView.setText("Sending Image to the Server");

                        FaceMatchClient.post(":8080/match", params, new JsonHttpResponseHandler() {
                            @Override
                            public void onSuccess(JSONArray result) {
                                Log.d("face onSuccess", result.toString());

                                try {
                                    JSONObject myJson = (JSONObject) result.get(0);
                                    float dist = (float) Double.parseDouble(myJson.getString("dist"));
                                    Log.d("distance", "" + dist);
                                    int level = (int) ((1 - dist) * 100);
                                    if (level > previousMatchLevel) {
                                        textView.setText("Match " + level + "% with " + myJson.getString("name")
                                                + " <" + myJson.getString("email") + "> ");

                                        loadImage(myJson.getString("classes"), myJson.getString("username"));
                                    }

                                    previousMatchLevel = level;
                                    trialCounter++;

                                    if (trialCounter < 100 && level < 74) {
                                        textServerView.setText("Retrying...");
                                        notRequesting = true;
                                    } else if (trialCounter == 100) {
                                        textServerView.setText("Fail...");
                                    } else {
                                        textServerView.setText("Found Good Match? If not try again!");
                                        fdButtonClicked = false;
                                        trialCounter = 0;
                                        previousMatchLevel = 0;
                                        mCamera.stopFaceDetection();
                                        button.setText("StartFaceDetection");
                                    }

                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                                //                                        informationView.showInfo(myJson);
                            }

                        });
                    }

                    textServerView.setText("POST Sent");
                    textServerView.setText("Awaiting for response");

                } catch (Exception e) {
                    e.printStackTrace();
                    textServerView.setText("Error AsyncPOST");
                }
            }
        }
    };

    // Open the default i.e. the first rear facing camera.
    mCamera = Camera.open();
    mCamera.setPreviewCallback(previewCallback);

    // To use front camera
    //        mCamera = Camera.open(CameraActivity.getFrontCameraId());
    mPreview.setCamera(mCamera);
    parameters = mCamera.getParameters();
    imageFormat = parameters.getPreviewFormat();
    PreviewSize = parameters.getPreviewSize();
}

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;//from  www .  ja  v a 2s.  c o m
    }

    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:org.akvo.caddisfly.ui.activity.MainActivity.java

private boolean checkCameraFlash() {
    boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    Camera camera = getCameraInstance();
    try {//from   ww w.j  a v a2s. co m
        Camera.Parameters p;

        if (hasFlash) {
            p = camera.getParameters();
            if (p.getSupportedFlashModes() == null) {
                hasFlash = false;
            } else {
                if (p.getSupportedFlashModes().size() == 1) {
                    if (p.getSupportedFlashModes().get(0).equals("off")) {
                        hasFlash = false;
                    }
                }
            }
        }
    } finally {
        if (camera != null) {
            camera.release();
        }
    }
    return hasFlash;
}

From source file:com.dngames.mobilewebcam.PhotoSettings.java

public void SetCameraParameters(Camera cam, boolean configure) {
    Camera.Parameters params = cam.getParameters();
    if (params != null) {
        SetCameraParameters(params, configure);
        try {/*from   ww w.ja  v a  2 s .c  o m*/
            cam.setParameters(params);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
}

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

private void initCamera(Camera camera) {
    Camera.Parameters cameraParameters = camera.getParameters();

    defaultFocusMode = cameraParameters.getFocusMode();

    continousFocusSupported = cameraParameters.getSupportedFocusModes()
            .contains(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    focusAreaSupported = (cameraParameters.getMaxNumFocusAreas() > 0
            && Util.isSupported(Parameters.FOCUS_MODE_AUTO, cameraParameters.getSupportedFocusModes()));
    meteringAreaSupported = (cameraParameters.getMaxNumMeteringAreas() > 0);
    aeLockSupported = cameraParameters.isAutoExposureLockSupported();
    awbLockSupported = cameraParameters.isAutoWhiteBalanceLockSupported();

    if (continousFocusSupported) {
        cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    } else if (Util.isSupported(Parameters.FOCUS_MODE_AUTO, cameraParameters.getSupportedFocusModes())) {
        cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    }/* w w  w  . j a  v a  2s.  co  m*/

    //      camera.enableShutterSound(true);  //requires api 17
    int camWidth = isPortraitOrientation() ? cameraView.getHeight() : cameraView.getWidth();
    int camHt = isPortraitOrientation() ? cameraView.getWidth() : cameraView.getHeight();
    previewSizePair = selectSizePair(cameraParameters.getPreferredPreviewSizeForVideo(),
            cameraParameters.getSupportedPreviewSizes(), cameraParameters.getSupportedPictureSizes(), camWidth,
            camHt);

    Log.d(TAG, "preview size: " + previewSizePair.previewSize.width + ":" + previewSizePair.previewSize.height);

    cameraParameters.setPreviewSize(previewSizePair.previewSize.width, previewSizePair.previewSize.height);

    //commenting out; not used now
    //Camera.Size picSize = previewSizePair.pictureSize != null ? previewSizePair.pictureSize : previewSizePair.previewSize;
    //cameraParameters.setPictureSize(picSize.width,picSize.height);
    //Log.d(TAG, "picture size: " + picSize.width + ":" + picSize.height);

    camera.setParameters(cameraParameters);

    try {
        if (cameraView.getSurfaceTexture() != null) {
            camera.setPreviewTexture(cameraView.getSurfaceTexture());
        }
    } catch (IOException e) {
        Log.e(TAG, "Unable to attach preview to camera!", e);
    }
}

From source file:com.android.launcher3.Utilities.java

public static void turnOnFlashLight(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        CameraManager camManager = (CameraManager) context.getApplicationContext()
                .getSystemService(Context.CAMERA_SERVICE);
        String cameraId = null;// w w  w .jav a  2 s  .  c o m
        try {
            cameraId = camManager.getCameraIdList()[0];
            camManager.setTorchMode(cameraId, true);
            isFlashLightOn = true;
        } catch (CameraAccessException e) {
            isFlashLightOn = false;
            e.printStackTrace();
        }
    } else {
        try {
            if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
                Camera cam = Camera.open();
                Camera.Parameters p = cam.getParameters();
                p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();
                isFlashLightOn = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            isFlashLightOn = false;
        }
    }
}