List of usage examples for android.hardware Camera open
public static Camera open(int cameraId)
From source file:cn.pjt.rxjava.camera.CameraPreviewFragment.java
/** * A safe way to get an instance of the Camera object. *//*www.jav a2 s .c o m*/ public static 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) Log.d(TAG, "Camera " + cameraId + " is not available: " + e.getMessage()); } return c; // returns null if camera is unavailable }
From source file:com.actionbarsherlock.sample.hcgallery.CameraFragment.java
@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);/*from w ww. ja v a2s . c o m*/ }
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 w ww. j ava 2 s . co 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:com.fallahpoor.infocenter.fragments.CameraFragment.java
private Camera getCameraInstance(int cameraId) { Camera camera = null;//from w ww. ja v a2s. c om try { camera = Camera.open(cameraId); } catch (Exception ignored) { } return camera; }
From source file:com.actionbarsherlock.sample.hcgallery.CameraFragment.java
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.switch_cam: // Release this camera -> mCameraCurrentlyLocked if (mCamera != null) { mCamera.stopPreview();//from www . ja v a 2 s . c o m mPreview.setCamera(null); mCamera.release(); mCamera = null; } // Acquire the next camera and request Preview to reconfigure // parameters. mCamera = Camera.open((mCameraCurrentlyLocked + 1) % mNumberOfCameras); mCameraCurrentlyLocked = (mCameraCurrentlyLocked + 1) % mNumberOfCameras; mPreview.switchCamera(mCamera); // Start the preview mCamera.startPreview(); return true; case android.R.id.home: Intent intent = new Intent(this.getActivity(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent); default: return super.onOptionsItemSelected(item); } }
From source file:com.jasompeter.openalpr.CameraActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_camera); SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface_view); mTorchButton = (ImageButton) findViewById(R.id.torch_button); mCapturingText = (TextView) findViewById(R.id.capturing_text); mTouchToCaptureText = (TextView) findViewById(R.id.touch_to_capture); mSurfaceHolder = surfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // we should not open camera on UI thread mCameraOpenTask = new AsyncTask<Void, Void, Void>() { @Override/*from w ww. j a v a 2 s.c o m*/ protected Void doInBackground(Void[] params) { if (!mCameraIsOpen) { releaseCameraAndPreview(); try { int cameraCount = Camera.getNumberOfCameras(); Log.d(TAG, "We have " + cameraCount + " cameras."); for (int i = 0; i < cameraCount; i++) { Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); Camera.getCameraInfo(i, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { mCameraId = i; break; } } mCamera = Camera.open(mCameraId); mCameraIsOpen = true; } catch (Exception e) { mCameraIsOpen = false; Log.d(TAG, "Failed to open camera. Camera is probably in use."); } } return null; } @Override protected void onPostExecute(Void o) { onCameraOpened(mCamera != null); } }; // orientation manager handles orientation changes // because our activity is set to force landscape mode // and we may want rotate action buttons or do something with view mOrientationManager = new OrientationManager(this, SensorManager.SENSOR_DELAY_NORMAL, this); invalidateTorchButton(); mTorchButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { toggleTorch(); } }); initializeAlpr(); surfaceView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mEnableRecognition = true; mCapturingText.setVisibility(View.VISIBLE); mTouchToCaptureText.setVisibility(View.GONE); return true; // if you want to handle the touch event case MotionEvent.ACTION_UP: mEnableRecognition = false; mCapturingText.setVisibility(View.GONE); mTouchToCaptureText.setVisibility(View.VISIBLE); return true; // if you want to handle the touch event } return false; } }); }
From source file:com.mobiuso.argo.ARModule.CameraSurface.java
@SuppressLint("NewApi") @Override// ww w. j a va 2s. co m public void surfaceCreated(SurfaceHolder holder) { Log.i(TAG, "Opening camera."); try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { int cameraIndex = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(getContext()) .getString("pref_cameraIndex", "0")); camera = Camera.open(cameraIndex); } else { camera = Camera.open(); } } catch (RuntimeException exception) { Log.e(TAG, "Cannot open camera. It may be in use by another process."); } if (camera != null) { try { camera.setPreviewDisplay(holder); //camera.setPreviewCallback(this); camera.setPreviewCallbackWithBuffer(this); // API level 8 (Android 2.2) } catch (IOException exception) { Log.e(TAG, "Cannot set camera preview display."); camera.release(); camera = null; } } }
From source file:com.wlanjie.streaming.camera.Camera1.java
private void openCamera() { if (mCamera != null) { releaseCamera();/*from www .ja v a 2 s .co m*/ } mCamera = Camera.open(mCameraId); mCameraParameters = mCamera.getParameters(); // Supported preview sizes mPreviewSizes.clear(); for (Camera.Size size : mCameraParameters.getSupportedPreviewSizes()) { mPreviewSizes.add(new Size(size.width, size.height)); } // Supported picture sizes; mPictureSizes.clear(); for (Camera.Size size : mCameraParameters.getSupportedPictureSizes()) { mPictureSizes.add(new Size(size.width, size.height)); } // AspectRatio if (mAspectRatio == null) { mAspectRatio = Constants.DEFAULT_ASPECT_RATIO; } adjustCameraParameters(); mCamera.setDisplayOrientation(calcCameraRotation(mDisplayOrientation)); Camera.Size size = mCamera.getParameters().getPreviewSize(); mCallback.onCameraOpened(size.width, size.height); }
From source file:com.czh.testmpeg.videorecord.CameraActivity.java
public void onResume() { super.onResume(); if (!hasCamera(getApplicationContext())) { //???/*w ww .j a v a 2 s . com*/ Toast.makeText(getApplicationContext(), R.string.dont_have_camera_error, Toast.LENGTH_SHORT).show(); setResult(MainActivity.RESULT_CODE_FOR_RECORD_VIDEO_FAILED); releaseCamera(); releaseMediaRecorder(); finish(); } if (mCamera == null) { releaseCamera(); final boolean frontal = cameraFront; int cameraId = findFrontFacingCamera(); if (cameraId < 0) { //??? switchCameraListener = new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(CameraActivity.this, R.string.dont_have_front_camera, Toast.LENGTH_SHORT) .show(); } }; //??? cameraId = findBackFacingCamera(); if (flash) { mPreview.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); mBinding.buttonFlash.setImageResource(R.mipmap.ic_flash_on_white); } } else if (!frontal) { cameraId = findBackFacingCamera(); if (flash) { mPreview.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); mBinding.buttonFlash.setImageResource(R.mipmap.ic_flash_on_white); } } mCamera = Camera.open(cameraId); mPreview.refreshCamera(mCamera); reloadQualities(cameraId); } }
From source file:ru.jango.j0widget.camera.fragment.AbstractCameraFragment.java
/** * Place, where {@link android.hardware.Camera} object is obtained and configured. Reload it for * more configurations./* w w w . j a va 2s. c om*/ */ protected void openCamera() { camera = Camera.open(cameraId); final List<Camera.Size> sizes = camera.getParameters().getSupportedPictureSizes(); final Camera.Size optimal = getOptimalSize(sizes); final Camera.Parameters params = camera.getParameters(); params.setPictureSize(optimal.width, optimal.height); camera.setParameters(params); }