Java Code Examples for android.hardware.Camera#open()

The following examples show how to use android.hardware.Camera#open() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CameraManager.java    From letv with Apache License 2.0 8 votes vote down vote up
public void openDriver(SurfaceHolder holder) throws IOException {
    if (this.camera == null) {
        try {
            this.camera = Camera.open();
        } catch (Exception e) {
        }
        Logger.e(TAG, "camera打开");
        if (this.camera == null) {
            throw new IOException();
        }
        this.camera.setPreviewDisplay(holder);
        if (!this.initialized) {
            this.initialized = true;
            this.configManager.initFromCameraParameters(this.camera);
        }
        this.configManager.setDesiredCameraParameters(this.camera);
    }
}
 
Example 2
Source File: CameraActivity.java    From vocefiscal-android with Apache License 2.0 6 votes vote down vote up
/** A safe way to get an instance of the Camera object. */
public Camera getCameraInstance()
{
	Camera c = null;
	try 
	{
		c = Camera.open(); // attempt to get a Camera instance
	}
	catch (Exception e)
	{
		// Camera is not available (in use or does not exist)
		Toast.makeText(getApplicationContext(), "Não foi possível ter acesso à câmera do celular.", Toast.LENGTH_SHORT).show();
		setResult(RESULT_CANCELED);
		
		finish();
	}
	return c; // returns null if camera is unavailable
}
 
Example 3
Source File: CameraActivity.java    From privacy-friendly-shopping-list with Apache License 2.0 6 votes vote down vote up
public Camera getInitializedCamera()
{
    Camera camera = null;
    try
    {
        camera = Camera.open();
        cameraOrientation = getWindowManager().getDefaultDisplay().getRotation();
        int orientation = CameraUtils.getRotationAdjustment(cameraOrientation);
        camera.setDisplayOrientation(orientation);
        setAutoFocus(camera);
        photoCaptured = false;
    }
    catch ( Exception e )
    {
        // no camera available
    }
    return camera;
}
 
Example 4
Source File: CameraViewProxy.java    From Ti-Android-CameraView with MIT License 6 votes vote down vote up
public Camera getCameraInstance()
{
	Camera c = null;
	try
	{
		if( FRONT_CAMERA && hasFrontCamera() ) {
			Log.i(TAG, "Using Front Camera");
			c = Camera.open( Camera.CameraInfo.CAMERA_FACING_FRONT );
		} else {
			Log.i(TAG, "Using Back Camera");
			c = Camera.open();
		}
	}
	catch( Exception e )
	{
		Log.d(TAG, "Camera not available");
	}
	return c;
}
 
Example 5
Source File: FlashlightUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 注册摄像头
 * @return {@code true} success, {@code false} fail
 */
public boolean register() {
    try {
        mCamera = Camera.open(0);
    } catch (Throwable ignore) {
        return false;
    }
    if (mCamera == null) return false;
    try {
        mCamera.setPreviewTexture(new SurfaceTexture(0));
        mCamera.startPreview();
        return true;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "register");
        return false;
    }
}
 
Example 6
Source File: CameraInterface.java    From imsdk-android with MIT License 6 votes vote down vote up
private synchronized void openCamera(int id) {
    try {
        this.mCamera = Camera.open(id);
    } catch (Exception var3) {
        var3.printStackTrace();
        if (this.errorLisenter != null) {
            this.errorLisenter.onError();
        }
    }

    if (Build.VERSION.SDK_INT > 17 && this.mCamera != null) {
        try {
            this.mCamera.enableShutterSound(false);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "enable shutter sound faild");
        }
    }
}
 
Example 7
Source File: LiveCameraActivity.java    From grafika with Apache License 2.0 6 votes vote down vote up
private void startPreview() {
    mCamera = Camera.open();
    if (mCamera == null) {
        // Seeing this on Nexus 7 2012 -- I guess it wants a rear-facing camera, but
        // there isn't one.  TODO: fix
        throw new RuntimeException("Default camera not available");
    }

    try {
        mCamera.setPreviewTexture(mSurfaceTexture);
        Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

        if(display.getRotation() == Surface.ROTATION_0) {
            mCamera.setDisplayOrientation(90);
        }
        if(display.getRotation() == Surface.ROTATION_270) {
            mCamera.setDisplayOrientation(180);
        }
        mCamera.startPreview();
    } catch (IOException ioe) {
        // Something bad happened
        Log.e(TAG,"Exception starting preview", ioe);
    }
}
 
Example 8
Source File: VideoPusher.java    From LivePublisher with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void startPreview() {
	if (mPreviewRunning) {
		return;
	}
	try {
		mCamera = Camera.open(mParam.getCameraId());
		Camera.Parameters parameters = mCamera.getParameters();
		parameters.setPreviewFormat(ImageFormat.NV21);
		setPreviewSize(parameters);
		setPreviewFpsRange(parameters);
		setPreviewOrientation(parameters);
		mCamera.setParameters(parameters);
		buffer = new byte[mParam.getWidth() * mParam.getHeight() * 3 / 2];
		raw = new byte[mParam.getWidth() * mParam.getHeight() * 3 / 2];
		mCamera.addCallbackBuffer(buffer);
		mCamera.setPreviewCallbackWithBuffer(this);
		mCamera.setPreviewDisplay(mHolder);
		mCamera.startPreview();
		mPreviewRunning = true;
	} catch (Exception ex) {
		ex.printStackTrace();
		if (null != mListener) {
			mListener.onErrorPusher(-100);
		}
	}
}
 
Example 9
Source File: CustomVideoCapturer.java    From opentok-android-sdk-samples with MIT License 5 votes vote down vote up
public synchronized void init() {
    Log.d(LOG_TAG, "init() enetered");
    try {
        camera = Camera.open(cameraIndex);
    } catch (RuntimeException exp) {
        Log.e(LOG_TAG, "The camera is in use by another app");
    }

    currentDeviceInfo = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraIndex, currentDeviceInfo);
    Log.d(LOG_TAG, "init() exit");
}
 
Example 10
Source File: CameraFragment.java    From androidtestdebug with MIT License 5 votes vote down vote up
@Override
public void onResume() {
    super.onResume();

    // Open the default i.e. the first rear facing camera.
    mCamera = Camera.open(mDefaultCameraId);
    mCameraCurrentlyLocked = mDefaultCameraId;
    mPreview.setCamera(mCamera);
}
 
Example 11
Source File: RecorderVideoActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@SuppressLint("NewApi")
public void switchCamera() {

	if (mCamera == null) {
		return;
	}
	if (Camera.getNumberOfCameras() >= 2) {
		btn_switch.setEnabled(false);
		if (mCamera != null) {
			mCamera.stopPreview();
			mCamera.release();
			mCamera = null;
		}

		switch (frontCamera) {
		case 0:
			mCamera = Camera.open(CameraInfo.CAMERA_FACING_FRONT);
			frontCamera = 1;
			break;
		case 1:
			mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
			frontCamera = 0;
			break;
		}
		try {
			mCamera.lock();
			mCamera.setDisplayOrientation(90);
			mCamera.setPreviewDisplay(mVideoView.getHolder());
			mCamera.startPreview();
		} catch (IOException e) {
			mCamera.release();
			mCamera = null;
		}
		btn_switch.setEnabled(true);

	}

}
 
Example 12
Source File: MapsActivity.java    From Self-Driving-Car with MIT License 5 votes vote down vote up
/**
 * A safe way to get an instance of the Camera object.
 */
private Camera getCameraInstance(int cameraId) {
    Camera c = null;
    try {
        c = Camera.open(cameraId); // attempt to get a Camera instance
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
        Toast.makeText(this, "Camera " + cameraId + " is not available: " + e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }
    return c; // returns null if camera is unavailable
}
 
Example 13
Source File: CameraMetricsCollectorTest.java    From Battery-Metrics with MIT License 5 votes vote down vote up
@Test
public void testIncompletePreviewSnapshot() {
  ShadowSystemClock.setUptimeMillis(700);
  CameraMetricsCollector collector = new CameraMetricsCollector();
  Camera testCamera = Camera.open();
  collector.recordPreviewStart(testCamera);

  ShadowSystemClock.setUptimeMillis(1000);
  CameraMetrics snapshot = new CameraMetrics();
  assertThat(collector.getSnapshot(snapshot)).isTrue();
  assertThat(snapshot.cameraOpenTimeMs).isEqualTo(0);
  assertThat(snapshot.cameraPreviewTimeMs).isEqualTo(300);
}
 
Example 14
Source File: NativeCamera.java    From VideoCamera with Apache License 2.0 4 votes vote down vote up
public void openNativeCamera() throws RuntimeException {
    camera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
}
 
Example 15
Source File: CameraSurfaceView.java    From Paddle-Lite-Demo with Apache License 2.0 4 votes vote down vote up
public void openCamera() {
    if (disableCamera) return;
    camera = Camera.open(selectedCameraId);
    List<Size> supportedPreviewSizes = camera.getParameters().getSupportedPreviewSizes();
    Size previewSize = Utils.getOptimalPreviewSize(supportedPreviewSizes, EXPECTED_PREVIEW_WIDTH,
            EXPECTED_PREVIEW_HEIGHT);
    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewSize(previewSize.width, previewSize.height);
    if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    }
    camera.setParameters(parameters);
    int degree = Utils.getCameraDisplayOrientation(context, selectedCameraId);
    camera.setDisplayOrientation(degree);
    boolean rotate = degree == 90 || degree == 270;
    textureWidth = rotate ? previewSize.height : previewSize.width;
    textureHeight = rotate ? previewSize.width : previewSize.height;
    // Destroy FBO and draw textures
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
    GLES20.glDeleteFramebuffers(1, fbo, 0);
    GLES20.glDeleteTextures(1, drawTexureId, 0);
    GLES20.glDeleteTextures(1, fboTexureId, 0);
    // Normal texture for storing modified camera preview data(RGBA format)
    GLES20.glGenTextures(1, drawTexureId, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, drawTexureId[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, textureWidth, textureHeight, 0,
            GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
    // FBO texture for storing camera preview data(RGBA format)
    GLES20.glGenTextures(1, fboTexureId, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, fboTexureId[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, textureWidth, textureHeight, 0,
            GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
    // Generate FBO and bind to FBO texture
    GLES20.glGenFramebuffers(1, fbo, 0);
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fbo[0]);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D,
            fboTexureId[0], 0);
    try {
        camera.setPreviewTexture(surfaceTexture);
    } catch (IOException exception) {
        Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
    }
    camera.startPreview();
}
 
Example 16
Source File: CameraHelper.java    From android-BasicMediaDecoder with Apache License 2.0 4 votes vote down vote up
/**
 * @return the default camera on the device. Return null if there is no camera on the device.
 */
public static Camera getDefaultCameraInstance() {
    return Camera.open();
}
 
Example 17
Source File: OpenCameraInterface.java    From ProjectX with Apache License 2.0 4 votes vote down vote up
/**
 * Opens the requested camera with {@link Camera#open(int)}, if one exists.
 *
 * @param cameraId camera ID of the camera to use. A negative value
 *                 or {@link #NO_REQUESTED_CAMERA} means "no preference", in which case a rear-facing
 *                 camera is returned if possible or else any camera
 * @return handle to {@link OpenCamera} that was opened
 */
public static OpenCamera open(int cameraId) {

    int numCameras = Camera.getNumberOfCameras();
    if (numCameras == 0) {
        Log.w(TAG, "No cameras!");
        return null;
    }

    boolean explicitRequest = cameraId >= 0;

    Camera.CameraInfo selectedCameraInfo = null;
    int index;
    if (explicitRequest) {
        index = cameraId;
        selectedCameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(index, selectedCameraInfo);
    } else {
        index = 0;
        while (index < numCameras) {
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(index, cameraInfo);
            CameraFacing reportedFacing = CameraFacing.values()[cameraInfo.facing];
            if (reportedFacing == CameraFacing.BACK) {
                selectedCameraInfo = cameraInfo;
                break;
            }
            index++;
        }
    }

    Camera camera;
    if (index < numCameras) {
        Log.i(TAG, "Opening camera #" + index);
        camera = Camera.open(index);
    } else {
        if (explicitRequest) {
            Log.w(TAG, "Requested camera does not exist: " + cameraId);
            camera = null;
        } else {
            Log.i(TAG, "No camera facing " + CameraFacing.BACK + "; returning camera #0");
            camera = Camera.open(0);
            selectedCameraInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(0, selectedCameraInfo);
        }
    }

    if (camera == null) {
        return null;
    }
    return new OpenCamera(index,
            camera,
            CameraFacing.values()[selectedCameraInfo.facing],
            selectedCameraInfo.orientation);
}
 
Example 18
Source File: PreviewSurfaceView.java    From Android-Video-Trimmer with Apache License 2.0 4 votes vote down vote up
private void init() {
  mCamera = Camera.open(cameraId);
  mHolder = getHolder();
  mHolder.addCallback(this);
}
 
Example 19
Source File: CameraUtils9.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Camera openCamera(int index) {
    return Camera.open(index);
}
 
Example 20
Source File: CameraPreviewInput.java    From UltimateAndroid with Apache License 2.0 votes vote down vote up
protected Camera createCamera() {
		return Camera.open();
	}