Example usage for android.hardware Camera open

List of usage examples for android.hardware Camera open

Introduction

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

Prototype

public static Camera open() 

Source Link

Document

Creates a new Camera object to access the first back-facing camera on the device.

Usage

From source file:com.bilibili.boxing.utils.CameraPickerHelper.java

/**
 * start system camera to take a picture
 *
 * @param activity      not null if fragment is null.
 * @param fragment      not null if activity is null.
 * @param subFolderPath a folder in external DCIM,must start with "/".
 *///from w  w w.  j a va  2s .c o  m
public void startCamera(final Activity activity, final Fragment fragment, final String subFolderPath) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || !takePhotoSecure(activity, fragment, subFolderPath)) {
        FutureTask<Boolean> task = BoxingExecutor.getInstance().runWorker(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                try {
                    // try...try...try
                    Camera camera = Camera.open();
                    camera.release();
                } catch (Exception e) {
                    BoxingLog.d("camera is not available.");
                    return false;
                }
                return true;
            }
        });
        try {
            if (task != null && task.get()) {
                startCameraIntent(activity, fragment, subFolderPath, MediaStore.ACTION_IMAGE_CAPTURE,
                        REQ_CODE_CAMERA);
            } else {
                callbackError();
            }
        } catch (InterruptedException | ExecutionException ignore) {
            callbackError();
        }

    }
}

From source file:org.akvo.caddisfly.ui.activity.MainActivity.java

private static Camera getCameraInstance() {
    Camera c = null;/* ww w  .ja va 2s  .c  om*/
    try {
        c = Camera.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return c;
}

From source file:com.TwentyCodes.android.IOIOTruck.CameraFragment.java

/**
 * Called when the application is resuming
 * (non-Javadoc)//w  w w.  j av a2  s  .c o m
 * @see android.support.v4.app.Fragment#onResume()
 * @author ricky barrette 
 */
@Override
public void onResume() {
    super.onResume();

    /*
     * open camera
     */
    mCamera = Camera.open();
    mCamera.setDisplayOrientation(90);
    Camera.Parameters params = mCamera.getParameters();

    /*
     * find smallest preview size
     */
    List<Size> sizes = params.getSupportedPreviewSizes();
    Iterator<Size> iter = sizes.iterator();
    mPreviewSize = sizes.get(0);
    int minPixelCount = mPreviewSize.width * mPreviewSize.height;
    while (iter.hasNext()) {
        Size size = iter.next();
        int pixelCount = size.height * size.width;
        if (pixelCount < minPixelCount) {
            minPixelCount = pixelCount;
            mPreviewSize = size;
        }
    }
    params.setPreviewSize(mPreviewSize.width, mPreviewSize.height);

    mPreviewOverlay.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
    mImageData = new byte[mPreviewSize.width * mPreviewSize.height];
    mCamera.setPreviewCallback(new Camera.PreviewCallback() {

        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            if (!isDrawingView) {
                mImageData = data;
                mPreviewOverlay.invalidate();
                isDrawingView = true;
            }
        }
    });

    params.setRotation(90);
    mCamera.setParameters(params);

}

From source file:com.dynamsoft.camera.DBR.java

private static Camera getCameraInstance() {
    Camera c = null;// w w w.j av  a2 s . c  o m
    try {
        c = Camera.open(); // attempt to get a Camera instance
    } catch (Exception e) {
        Log.i(TAG, "Camera is not available (in use or does not exist)");
    }
    return c; // returns null if camera is unavailable
}

From source file:com.pimp.companionforband.fragments.extras.CameraActivity.java

@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {/*from   w  w  w. ja  v a 2 s.  c o m*/
        camera = Camera.open();
    } catch (RuntimeException e) {
        System.err.println(e);
        return;
    }

    Camera.Parameters params;
    params = camera.getParameters();

    List<Camera.Size> sizes = params.getSupportedPictureSizes();
    Camera.Size size = sizes.get(0);
    for (int i = 0; i < sizes.size(); i++) {
        if (sizes.get(i).width > size.width)
            size = sizes.get(i);
    }
    params.setPictureSize(size.width, size.height);
    params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
    params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    params.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);

    camera.setParameters(params);

    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (Exception e) {
        System.err.println(e);
    }
}

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

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

    // Open the default i.e. the first rear facing camera.
    mCamera = Camera.open();
    cameraCurrentlyLocked = defaultCameraId;
    mPreview.setCamera(mCamera);//from   w ww.  j a v a2s.c o m
}

From source file:havidarou.com.appgestosfoto.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Associate the textView
    helper = (TextView) findViewById(R.id.textHelper);

    // Associate the imageViews
    a1 = (ImageView) findViewById(R.id.imageView);
    a2 = (ImageView) findViewById(R.id.imageView2);
    a3 = (ImageView) findViewById(R.id.imageView3);
    b1 = (ImageView) findViewById(R.id.imageView4);
    b2 = (ImageView) findViewById(R.id.imageView5);
    b3 = (ImageView) findViewById(R.id.imageView6);
    c1 = (ImageView) findViewById(R.id.imageView7);
    c2 = (ImageView) findViewById(R.id.imageView8);
    c3 = (ImageView) findViewById(R.id.imageView9);

    // Adding the movement patterns
    movement.add(0);/* w ww . j a  v  a2  s . co  m*/

    zMovement.add(0);
    zMovement.add(1);
    zMovement.add(2);
    zMovement.add(3);
    zMovement.add(5);
    zMovement.add(7);
    zMovement.add(8);
    zMovement.add(9);

    lMovement.add(0);
    lMovement.add(1);
    lMovement.add(4);
    lMovement.add(7);
    lMovement.add(8);
    lMovement.add(9);

    mMovement.add(0);
    mMovement.add(7);
    mMovement.add(4);
    mMovement.add(1);
    mMovement.add(5);
    mMovement.add(3);
    mMovement.add(6);
    mMovement.add(9);

    nMovement.add(0);
    nMovement.add(7);
    nMovement.add(4);
    nMovement.add(1);
    nMovement.add(5);
    nMovement.add(9);
    nMovement.add(6);
    nMovement.add(3);

    vMovement.add(0);
    vMovement.add(1);
    vMovement.add(8);
    vMovement.add(3);

    textTimeLeft = (TextView) findViewById(R.id.TimeLeft); // This is the timer
    camera = Camera.open();

    view = (SurfaceView) findViewById(R.id.cpPreview);
    view.setVisibility(View.INVISIBLE);

}

From source file:net.microtrash.clapcamera.Preview.java

public void openCamera() {
    Log.d(TAG, "openCamera()");
    if (this.camera == null) {
        try {/*  w ww  .j a v a2s .  c om*/
            this.camera = Camera.open();
            this.camera.setErrorCallback(new ErrorCallback() {
                @Override
                public void onError(int error, Camera camera) {
                    Log.e(TAG, "error! code:" + error);
                    Toast.makeText(context, "Camera error occured: " + error, 8000).show();
                }
            });

            requestLayout(); // -> onSizeChanged() -> onLayout()
        } catch (Exception e) {
            String errorMessage = "Manufacturer: " + Build.MANUFACTURER + "; Model:" + Build.MODEL
                    + "; Camera: " + this.camera + "; Stacktrace:" + Tools.exception2String(e);
            Log.d(TAG, "error occured: " + errorMessage);

            Toast.makeText(context,
                    "Uuups, I am sorry! Could not connect to the camera device. Please restart me or your phone.",
                    8000).show();
            Crittercism.logHandledException(e);
        }
    }
}

From source file:com.example.hudpassthrough.BluetoothChat.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (D)//from   w w  w .  jav  a  2  s.  c  om
        Log.e(TAG, "+++ ON CREATE +++");

    // Set up the window layout
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

    // Set up the custom title
    mTitle = (TextView) findViewById(R.id.title_left_text);
    mTitle.setText(R.string.app_name);
    mTitle = (TextView) findViewById(R.id.title_right_text);

    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    mCamera = Camera.open();
    if (mCamera == null)
        Log.e("Camera", "mCamera is null!");

    //mCamera.enableShutterSound(false); //Questionable Legality!

    CamPreview camPreview = new CamPreview(this, mCamera);
    camPreview.setSurfaceTextureListener(camPreview);
    FrameLayout preview = (FrameLayout) findViewById(R.id.cameraView);
    preview.addView(camPreview);
    CamCallback camCallback = new CamCallback();
    mCamera.setPreviewCallback(camCallback);

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());

    people = new PeopleDB();
}

From source file:me.hammarstrom.imagerecognition.activities.MainActivity.java

/**
 * Helper method to get {@link Camera} instance
 *
 * @return camera//from w  ww . jav  a  2s. c  om
 */
private Camera getCameraInstance() {
    Camera camera = null;
    try {
        camera = Camera.open();

        // Set auto focus mode
        Camera.Parameters parameters = camera.getParameters();
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        camera.setParameters(parameters);

    } catch (Exception e) {
        // cannot get camera or does not exist
    }
    return camera;
}