private void initCamera() { if (!isPreview) { camera = Camera.open(); } if (camera != null && !isPreview) { streamIt = new StreamIt(); try { Camera.Parameters parameters = camera.getParameters(); parameters.setPreviewSize(screenWidth, screenHeight); // 设置预览照片的大小 parameters.setPreviewFpsRange(20, 30); // 每秒显示20~30帧 parameters.setPictureFormat(ImageFormat.NV21); // 设置图片格式 parameters.setPictureSize(screenWidth, screenHeight); // 设置照片的大小 camera.setPreviewDisplay(surfaceHolder); // 通过SurfaceView显示取景画面 camera.setPreviewCallback(streamIt); // 设置回调的类 camera.startPreview(); // 开始预览 camera.autoFocus(null); // 自动对焦 } catch (Exception e) { e.printStackTrace(); } isPreview = true; Thread postThread2 = new Thread(new postThread()); postThread2.start(); System.out.println("线程启动了@@@@@@@@@@@@@@@@@@@@@@@@@@"); } }
private void initPreview(int width, int height) { if (camera != null && previewHolder.getSurface() != null) { try { camera.setPreviewDisplay(previewHolder); } catch (Throwable t) { } if (!cameraConfigured) { Camera.Parameters parameters = camera.getParameters(); Camera.Size size = getBestPreviewSize(width, height, parameters); Camera.Size pictureSize = getLargestPictureSize(parameters); if (size != null && pictureSize != null) { if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO)) { // set the focus mode parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); } parameters.setPreviewSize(size.width, size.height); parameters.setPictureSize(pictureSize.width, pictureSize.height); parameters.setPictureFormat(ImageFormat.JPEG); camera.setParameters(parameters); cameraConfigured = true; } } } }
/** 给摄像头设置好一系列相关参数,并开始预览 */ private void initCamera() { azLog.log("isPreView", "" + isPreview); if (mCamera != null && !isPreview) { try { Camera.Parameters parameters = mCamera.getParameters(); // 设置预览照片的大小 parameters.setPreviewSize(mPreviewWidth, mPreviewHeight); // 设置预览照片时每秒显示多少帧的最小值和最大值 parameters.setPreviewFpsRange(mFrameMin, mFrameMax); // 设置图片格式 parameters.setPictureFormat(mImageFormat); // 设置JPG照片的质量 parameters.set("jpeg-quality", mImageQuality); // 设置照片的大小 parameters.setPictureSize(mPictureWidth, mPictureHeight); // 通过SurfaceView显示取景画面 mCamera.setPreviewDisplay(mSurfaceHolder); // 开始预览 mCamera.startPreview(); azLog.log("AzCamera", "摄像头打开"); } catch (IOException e) { e.printStackTrace(); } isPreview = true; } }
public void setCaptureQuality(int cameraType, String captureQuality) { Camera camera = _cameras.get(cameraType); if (camera == null) { return; } Camera.Parameters parameters = camera.getParameters(); Camera.Size pictureSize = null; switch (captureQuality) { case RCTCameraModule.RCT_CAMERA_CAPTURE_QUALITY_LOW: pictureSize = getSmallestSize(parameters.getSupportedPictureSizes()); break; case RCTCameraModule.RCT_CAMERA_CAPTURE_QUALITY_MEDIUM: List<Camera.Size> sizes = parameters.getSupportedPictureSizes(); pictureSize = sizes.get(sizes.size() / 2); break; case RCTCameraModule.RCT_CAMERA_CAPTURE_QUALITY_HIGH: pictureSize = getBestSize( parameters.getSupportedPictureSizes(), Integer.MAX_VALUE, Integer.MAX_VALUE); } if (pictureSize != null) { parameters.setPictureSize(pictureSize.width, pictureSize.height); camera.setParameters(parameters); } }
/** a wrapper to set a new picture size when taking a snapshot */ private void setNewSize(int formatIndex) { try { Camera.Parameters parameters = getCurrentParameters(); List<Camera.Size> supportedSizes = parameters.getSupportedPictureSizes(); Camera.Size optimalPictureSize = supportedSizes.get(formatIndex); parameters.setPictureSize(optimalPictureSize.width, optimalPictureSize.height); mCamera.setParameters(parameters); } catch (Exception e) { SYSLOG("FAILED TO SET the PARAMETERS"); } }
private void initCameraParameters() { Camera.Parameters parameters = mCamera.getParameters(); String mod = Build.MODEL; if (Build.VERSION.SDK_INT >= 8) { // MZ 180, other 90... if ("M9".equalsIgnoreCase(mod) || "MX".equalsIgnoreCase(mod)) { setDisplayOrientation(mCamera, 180); } else { setDisplayOrientation(mCamera, 90); } } else { parameters.set("orientation", "portrait"); parameters.set("rotation", 90); } if (cameraCurrentlyLocked == CameraInfo.CAMERA_FACING_FRONT) { ismCameraCanFlash = false; } else { ismCameraCanFlash = true; } if (!ismCameraCanFlash) { mBtnFlash1.setVisibility(View.INVISIBLE); mBtnFlash2.setVisibility(View.INVISIBLE); mBtnFlash3.setVisibility(View.INVISIBLE); } else { mBtnFlash1.setVisibility(View.VISIBLE); } List<String> focusModes = parameters.getSupportedFocusModes(); if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { // Autofocus mode is supported parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); } if (parameters.isZoomSupported()) { parameters.setZoom(parameters.getZoom()); // 测试通过 } Camera.Size previewSize = getFitParametersSize(parameters.getSupportedPreviewSizes()); parameters.setPreviewSize(previewSize.width, previewSize.height); Camera.Size pictureSize = getFitParametersSize(parameters.getSupportedPictureSizes()); parameters.setPictureSize(pictureSize.width, pictureSize.height); try { mCamera.setParameters(parameters); } catch (Exception e) { e.printStackTrace(); } }
// Used only in old camera interface (HALv3 don't use it) public void setCameraPictureSize() { Camera camera = CameraController.getCamera(); if (null == camera) return; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainScreen.getMainContext()); int jpegQuality = Integer.parseInt(prefs.getString(MainScreen.sJPEGQualityPref, "95")); Camera.Parameters cp = CameraController.getInstance().getCameraParameters(); cp.setPictureSize(MainScreen.getImageWidth(), MainScreen.getImageHeight()); cp.setJpegQuality(jpegQuality); try { CameraController.getInstance().setCameraParameters(cp); } catch (RuntimeException e) { Log.e("CameraTest", "MainScreen.setupCamera unable setParameters " + e.getMessage()); } }
public void takePicture(final PictureTransaction xact) { if (inPreview) { if (isAutoFocusing) { throw new IllegalStateException("Camera cannot take a picture while auto-focusing"); } else { previewParams = camera.getParameters(); Camera.Parameters pictureParams = camera.getParameters(); Camera.Size pictureSize = xact.host.getPictureSize(xact, pictureParams); pictureParams.setPictureSize(pictureSize.width, pictureSize.height); pictureParams.setPictureFormat(ImageFormat.JPEG); if (xact.flashMode != null) { pictureParams.setFlashMode(xact.flashMode); } if (!onOrientationChange.isEnabled()) { setCameraPictureOrientation(pictureParams); } camera.setParameters(xact.host.adjustPictureParameters(xact, pictureParams)); xact.cameraView = this; postDelayed( new Runnable() { @Override public void run() { try { camera.takePicture(xact, null, new PictureTransactionCallback(xact)); } catch (Exception e) { android.util.Log.e(getClass().getSimpleName(), "Exception taking a picture", e); // TODO get this out to library clients } } }, xact.host.getDeviceProfile().getPictureDelay()); inPreview = false; } } else { throw new IllegalStateException( "Preview mode must have started before you can take a picture"); } }
public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); parameters = camera.getParameters(); parameters.setPreviewSize(640, 480); parameters.setPreviewFrameRate(30); parameters.setJpegQuality(100); parameters.setFocusMode("continuous-video"); parameters.setPictureSize(2048, 1536); camera.setParameters(parameters); // /!!!!! try { camera.setPreviewDisplay(holder); } catch (IOException exception) { camera.release(); camera = null; } }
@Override public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); Size size = choosePictureSize(camera.getParameters().getSupportedPictureSizes()); Camera.Parameters params = camera.getParameters(); params.setPictureSize(size.width, size.height); params.setJpegQuality(100); params.setJpegThumbnailQuality(100); params.setRotation(mRotation); // set rotation to save the picture // TODO: set the camera image size that is uniform and small. camera.setParameters(params); } catch (IOException e) { Log.e(LOG, e.toString()); } }
void initCameraParameters() { Camera.Parameters parameters = mCamera.getParameters(); parameters.setPictureFormat(ImageFormat.JPEG); List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes(); // for(Size s:previewSizes){ // System.out.println("("+s.width+","+s.height+")"); // } rate = CameraUtil.getScreenRate(RegistCameraActivity.this); Size previewSize = CameraUtil.getPropPreviewSize(previewSizes, rate, 800); parameters.setPreviewSize(previewSize.width, previewSize.height); List<Camera.Size> pictureSizes = parameters.getSupportedPictureSizes(); Size pictureSize = CameraUtil.getPropPictureSize(pictureSizes, rate, 800); parameters.setPictureSize(pictureSize.width, pictureSize.height); if (CURRENT_CAMERA == CameraInfo.CAMERA_FACING_FRONT) { mCameraOrientation = 270; } else { mCameraOrientation = 90; } parameters.setRotation(mCameraOrientation); // parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); mCamera.setParameters(parameters); }
public void surfaceCreated(SurfaceHolder holder) { try { // Attempt to open camera _camera = Camera.open(); // Set desired camera parameters Camera.Parameters p = _camera.getParameters(); p.setPictureFormat(ImageFormat.JPEG); p.setPictureSize(_width, _height); p.setJpegQuality(_quality); _camera.setParameters(p); } catch (RuntimeException e) { Log.w(TAG, "Unable to connect to camera.", e); // If opening the camera fails, return empty image and exit final Intent intent = new Intent(); intent.setAction(PICTURE_INTENT); intent.putExtra(IMAGE_EXTRA, new byte[0]); sendBroadcast(intent); setResult(RESULT_CANCELED, intent); finish(); } }
private void openCamera() { try { closeCamera(); mCamera = Camera.open(); mCamera.setDisplayOrientation(90); // 设置横行录制 Camera.Parameters params = mCamera.getParameters(); params.setPictureSize(1280, 720); params.setPreviewSize(VideoWidth, VideoHeight); // params.setPreviewFpsRange(12000, 20000); mCamera.setParameters(params); mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); mCamera.setPreviewCallback(CameraThread.this); mCamera.autoFocus(null); // very important log("open camera"); } catch (IOException e) { log("开启Camera exception"); log(e.getMessage()); } catch (Exception e) { log("open 异常"); log(e.getMessage()); } }
private void setPictureSize(Camera.Parameters parameters, Quality quality, Ratio ratio) { Camera.Size size = pictureSizes.get(ratio).get(quality); if (size != null) { parameters.setPictureSize(size.width, size.height); } }
/** * This is called immediately after any structural changes (format or size) have been made to the * surface. You should at this point update the imagery in the surface. This method is always * called at least once, after surfaceCreated(SurfaceHolder). * * @param holder The surface holder * @param format Format for the surface * @param w The width * @param h The height */ @Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // Check if the preview is running, if it is stop the preview if (mPreviewRunning) { mCamera.stopPreview(); } String sizePref = ""; sizePref = prefs.getString("resolution", "Not Present"); String prevSizePref = ""; prevSizePref = prefs.getString("previewresolution", "Not Present"); Camera.Parameters p = mCamera.getParameters(); // If no camera size is found in the shared preferences get the largest // supported size and set the picture size to that size if (sizePref.equals("Not Present")) { List<Size> sizes = p.getSupportedPictureSizes(); Size s = sizes.get(0); for (Size size : sizes) { if (size.width > s.width) { s = size; } } p.setPictureSize(s.width, s.height); } else { // Set to the users specified camera size String[] userResolution = sizePref.split(":"); p.setPictureSize(Integer.valueOf(userResolution[0]), Integer.valueOf(userResolution[1])); } // If no preview size is found in the shared preferences get the first // supported size and set the preview size to that size if (prevSizePref.equals("Not Present")) { // Use the first supported preview size List<Size> previewSizes = p.getSupportedPreviewSizes(); p.setPreviewSize(previewSizes.get(0).width, previewSizes.get(0).height); } else { // Set to the users specified preview size String[] userResolution = prevSizePref.split(":"); p.setPreviewSize(Integer.valueOf(userResolution[0]), Integer.valueOf(userResolution[1])); } // Set the focus mode to infinity List<String> focusModes = p.getSupportedFocusModes(); for (String str : focusModes) { if (str.equalsIgnoreCase("infinity")) { p.setFocusMode(str); } } try { // Set the camera parameters mCamera.setParameters(p); } catch (Exception e) { Toast.makeText(ctx, "Error occurred while setting parameters...", Toast.LENGTH_SHORT).show(); } try { // Set the preview to the holder mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } // Start the preview mCamera.startPreview(); mPreviewRunning = true; }
@Override protected Boolean doInBackground(Void... args) { if (opened) return true; try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { // Choose the first back-facing camera boolean cameraAvailable = false; final CameraInfo cameraInfo = new CameraInfo(); int cameraId = 0; while (!cameraAvailable && cameraId < Camera.getNumberOfCameras()) { Camera.getCameraInfo(cameraId, cameraInfo); if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) cameraAvailable = true; else cameraId++; } if (!cameraAvailable) return false; // No back-facing camera available... camera = Camera.open(cameraId); cameraOrientation = cameraInfo.orientation; } else { camera = Camera.open(); cameraOrientation = 90; // TODO Is this correct? } if (camera == null) return false; // No camera available... final Camera.Parameters params = camera.getParameters(); // Set the maximum resolution based on the maximum heap size final int maxHeapSize = ((ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)) .getMemoryClass(); final int maxResolution = Math.min( 5 * 1024 * 1024, maxHeapSize * 1024 * 1024 / 4 / 3); // Each image might take up to 33% of the maximum heap size and 5 // megapixels at most // System.err.println("max heap size: " + maxHeapSize); // System.err.println("max resolution: " + maxResolution); // Choose the biggest possible picture size within the limits and the corresponding preview // sizes with the same ratio pictureSize = null; previewSizes = new ArrayList<Size>(); final List<Size> supportedPictureSizes = params.getSupportedPictureSizes(); final List<Size> supportedPreviewSizes = params.getSupportedPreviewSizes(); Collections.sort(supportedPictureSizes, Collections.reverseOrder(sizeComparator)); for (Size supportedPictureSize : supportedPictureSizes) { for (Size supportedPreviewSize : supportedPreviewSizes) { if (supportedPictureSize.width * supportedPreviewSize.height == supportedPictureSize.height * supportedPreviewSize.width && supportedPictureSize.width * supportedPictureSize.height <= maxResolution) { pictureSize = supportedPictureSize; previewSizes.add(supportedPreviewSize); } } if (pictureSize != null) break; } // System.err.println("Selected resolution: " + pictureSize.width + "x" + // pictureSize.height); Collections.sort(previewSizes, Collections.reverseOrder(sizeComparator)); if (pictureSize == null) { // No picture size ratio and preview size ratio match (shouldn't happen...) releaseCamera(); return false; } // Set parameters final List<String> supportedWhiteBalance = params.getSupportedWhiteBalance(); final List<String> supportedFlashModes = params.getSupportedFlashModes(); final List<String> supportedSceneModes = params.getSupportedSceneModes(); final List<String> supportedFocusModes = params.getSupportedFocusModes(); params.setPictureSize(pictureSize.width, pictureSize.height); params.setPreviewSize(previewSizes.get(0).width, previewSizes.get(0).height); if (supportedWhiteBalance != null && supportedWhiteBalance.contains(Camera.Parameters.WHITE_BALANCE_AUTO)) { params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO); } if (supportedFlashModes != null && supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO)) { params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO); } if (supportedSceneModes != null && supportedSceneModes.contains(Camera.Parameters.SCENE_MODE_AUTO)) { params.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO); } // TODO Decide the preferred focus mode order if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); autofocus = true; } else if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); autofocus = true; } else if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); autofocus = false; } else { autofocus = false; } camera.setParameters(params); } catch (Exception e) { releaseCamera(); return false; // Some error while opening the camera... } opened = true; return true; }
// (Re)start preview with the closest supported format to |width| x |height| @ |framerate|. private void startPreviewOnCameraThread(int width, int height, int framerate) { checkIsOnCameraThread(); Logging.d( TAG, "startPreviewOnCameraThread requested: " + width + "x" + height + "@" + framerate); if (camera == null) { Logging.e(TAG, "Calling startPreviewOnCameraThread on stopped camera."); return; } requestedWidth = width; requestedHeight = height; requestedFramerate = framerate; // Find closest supported format for |width| x |height| @ |framerate|. final Camera.Parameters parameters = camera.getParameters(); final int[] range = CameraEnumerationAndroid.getFramerateRange(parameters, framerate * 1000); final Camera.Size previewSize = CameraEnumerationAndroid.getClosestSupportedSize( parameters.getSupportedPreviewSizes(), width, height); final CaptureFormat captureFormat = new CaptureFormat( previewSize.width, previewSize.height, range[Camera.Parameters.PREVIEW_FPS_MIN_INDEX], range[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]); // Check if we are already using this capture format, then we don't need to do anything. if (captureFormat.equals(this.captureFormat)) { return; } // Update camera parameters. Logging.d(TAG, "isVideoStabilizationSupported: " + parameters.isVideoStabilizationSupported()); if (parameters.isVideoStabilizationSupported()) { parameters.setVideoStabilization(true); } // Note: setRecordingHint(true) actually decrease frame rate on N5. // parameters.setRecordingHint(true); if (captureFormat.maxFramerate > 0) { parameters.setPreviewFpsRange(captureFormat.minFramerate, captureFormat.maxFramerate); } parameters.setPreviewSize(captureFormat.width, captureFormat.height); parameters.setPreviewFormat(captureFormat.imageFormat); // Picture size is for taking pictures and not for preview/video, but we need to set it anyway // as a workaround for an aspect ratio problem on Nexus 7. final Camera.Size pictureSize = CameraEnumerationAndroid.getClosestSupportedSize( parameters.getSupportedPictureSizes(), width, height); parameters.setPictureSize(pictureSize.width, pictureSize.height); // Temporarily stop preview if it's already running. if (this.captureFormat != null) { camera.stopPreview(); dropNextFrame = true; // Calling |setPreviewCallbackWithBuffer| with null should clear the internal camera buffer // queue, but sometimes we receive a frame with the old resolution after this call anyway. camera.setPreviewCallbackWithBuffer(null); } // (Re)start preview. Logging.d(TAG, "Start capturing: " + captureFormat); this.captureFormat = captureFormat; List<String> focusModes = parameters.getSupportedFocusModes(); if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); } camera.setParameters(parameters); if (!isCapturingToTexture) { videoBuffers.queueCameraBuffers(captureFormat.frameSize(), camera); camera.setPreviewCallbackWithBuffer(this); } camera.startPreview(); }
/** * Opens the camera and applies the user settings. * * @throws RuntimeException if the method fails */ @SuppressLint("InlinedApi") private Camera createCamera() { int requestedCameraId = getIdForRequestedCamera(mFacing); if (requestedCameraId == -1) { throw new RuntimeException(mContext.getString(R.string.no_requested_camera)); } Camera camera = Camera.open(requestedCameraId); SizePair sizePair = selectSizePair(camera, mRequestedPreviewWidth, mRequestedPreviewHeight, mContext); if (sizePair == null) { throw new RuntimeException(mContext.getString(R.string.no_suitable_preview_size)); } Size pictureSize = sizePair.pictureSize(); mPreviewSize = sizePair.previewSize(); int[] previewFpsRange = selectPreviewFpsRange(camera, mRequestedFps); if (previewFpsRange == null) { throw new RuntimeException(mContext.getString(R.string.no_suitable_frames_per_second)); } Camera.Parameters parameters = camera.getParameters(); parameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight()); parameters.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); parameters.setPreviewFpsRange( previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX], previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]); parameters.setPreviewFormat(ImageFormat.NV21); setRotation(camera, parameters, requestedCameraId); if (mFocusMode != null) { if (parameters.getSupportedFocusModes().contains(mFocusMode)) { parameters.setFocusMode(mFocusMode); } else { Log.i( TAG, mContext.getString(R.string.camera_focus_mode) + mFocusMode + mContext.getString(R.string.not_supported)); } } // setting mFocusMode to the one set in the params mFocusMode = parameters.getFocusMode(); if (mFlashMode != null) { if (parameters.getSupportedFlashModes().contains(mFlashMode)) { parameters.setFlashMode(mFlashMode); } else { Log.i( TAG, mContext.getString(R.string.flash_mode) + mFlashMode + mContext.getString(R.string.not_supported)); } } // setting mFlashMode to the one set in the params mFlashMode = parameters.getFlashMode(); camera.setParameters(parameters); // Four frame buffers are needed for working with the camera: // // one for the frame that is currently being executed upon in doing detection // one for the next pending frame to process immediately upon completing detection // two for the frames that the camera uses to populate future preview images camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback()); camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize)); camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize)); camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize)); camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize)); return camera; }