public void setCameraDisplaySetting() { // //Set Orentation if (!devicearraydata[2].equals("BYPASS")) { // --> For Camera Orentation Support cam.setDisplayOrientation(Integer.parseInt(devicearraydata[2])); } // //Set Camera Preview // Parameters param = cam.getParameters(); // param.setPreviewSize(Integer.parseInt(devicearraydata[4]), // Integer.parseInt(devicearraydata[3])); try { // cam.setParameters(param); setPictureSizeOfScreenAndPreviewsize(); } catch (Exception e) { PrintLog.print(activity, "Can't setPreviewSize"); Parameters param = cam.getParameters(); param.setPreviewSize(480, 640); cam.setParameters(param); PrintLog.print(activity, "set lowest PreviewSize 480x640"); } PrintLog.print(activity.getApplicationContext(), Deviceinfo.BUILD_MODEL); PrintLog.print( activity.getApplicationContext(), Deviceinfo.getDeviceScreenOrentation(activity)); PrintLog.print( activity.getApplicationContext(), "W" + Deviceinfo.getDeviceScreenSize(activity)[0] + "|H" + Deviceinfo.getDeviceScreenSize(activity)[1] + "|A" + Deviceinfo.getStatusBarHeight(activity)); }
/** * Opens the camera and starts sending preview frames to the underlying detector. The preview * frames are not displayed. * * @throws IOException if the camera's preview texture or display could not be initialized */ @RequiresPermission(Manifest.permission.CAMERA) public CameraSource start() throws IOException { synchronized (mCameraLock) { if (mCamera != null) { return this; } mCamera = createCamera(); // SurfaceTexture was introduced in Honeycomb (11), so if we are running and // old version of Android. fall back to use SurfaceView. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mDummySurfaceTexture = new SurfaceTexture(DUMMY_TEXTURE_NAME); mCamera.setPreviewTexture(mDummySurfaceTexture); } else { mDummySurfaceView = new SurfaceView(mContext); mCamera.setPreviewDisplay(mDummySurfaceView.getHolder()); } mCamera.startPreview(); mProcessingThread = new Thread(mFrameProcessor); mFrameProcessor.setActive(true); mProcessingThread.start(); } return this; }
private boolean openCamera(int id) { boolean result = false; cameraId = id; releaseCamera(); try { camera = Camera.open(cameraId); } catch (Exception e) { e.printStackTrace(); } if (camera != null) { try { setUpCamera(camera); camera.setErrorCallback( new ErrorCallback() { @Override public void onError(int error, Camera camera) {} }); camera.setPreviewDisplay(surfaceHolder); camera.startPreview(); result = true; } catch (IOException e) { e.printStackTrace(); result = false; releaseCamera(); } } return result; }
public void onPreviewFrame(byte[] data, Camera camera) { Camera.Parameters parameters = camera.getParameters(); Camera.Size size = parameters.getPreviewSize(); Image barcode = new Image(size.width, size.height, "Y800"); barcode.setData(data); int result = mScanner.scanImage(barcode); if (result != 0) { mCamera.cancelAutoFocus(); mCamera.setPreviewCallback(null); mCamera.stopPreview(); mPreviewing = false; SymbolSet syms = mScanner.getResults(); for (Symbol sym : syms) { String symData = sym.getData(); if (!TextUtils.isEmpty(symData)) { Intent dataIntent = new Intent(); dataIntent.putExtra(SCAN_RESULT, symData); dataIntent.putExtra(SCAN_RESULT_TYPE, sym.getType()); setResult(Activity.RESULT_OK, dataIntent); finish(); break; } } } }
/* (non-Javadoc) * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder) */ @Override public void surfaceDestroyed(SurfaceHolder holder) { // Release the camera resources mCamera.stopPreview(); mCamera.release(); mCamera = null; }
/** Asks the camera hardware to begin drawing preview frames to the screen. */ public void startPreview() { if (camera != null && !previewing) { camera.setDisplayOrientation(90); camera.startPreview(); previewing = true; } }
private void checkRearCamera() { Camera camera = null; try { camera = Camera.open(); if (camera != null) { assertAvailable(PackageManager.FEATURE_CAMERA); Camera.Parameters params = camera.getParameters(); if (params.getSupportedFocusModes().contains(Parameters.FOCUS_MODE_AUTO)) { assertAvailable(PackageManager.FEATURE_CAMERA_AUTOFOCUS); } else { assertNotAvailable(PackageManager.FEATURE_CAMERA_AUTOFOCUS); } if (params.getFlashMode() != null) { assertAvailable(PackageManager.FEATURE_CAMERA_FLASH); } else { assertNotAvailable(PackageManager.FEATURE_CAMERA_FLASH); } } else { assertNotAvailable(PackageManager.FEATURE_CAMERA); assertNotAvailable(PackageManager.FEATURE_CAMERA_AUTOFOCUS); assertNotAvailable(PackageManager.FEATURE_CAMERA_FLASH); } } finally { if (camera != null) { camera.release(); } } }
/** **************************************************************** about Camera */ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.d(TAG, "---->>>>>>>>>> surfaceChanged()"); if (holder.getSurface() == null) { Log.d(TAG, "---- surfaceChanged(), holder.getSurface() == null"); return; } mSurfaceHolder = holder; try { hasCamera = true; nocamera.setVisibility(View.GONE); mCameraDevice = Camera.open(mCurrentCameraId); } catch (Exception e) { hasCamera = false; nocamera.setVisibility(View.VISIBLE); mSwitchBut.setVisibility(View.GONE); Log.e(TAG, " ____________- camera error"); return; } if (mHardwareRotation == 0) // XXX mCameraDevice.setDisplayOrientation(90); if (mCurrentCameraId == 1) mCameraDevice.setDisplayOrientation(270); // XXX try { mCameraDevice.setPreviewDisplay(mSurfaceHolder); } catch (IOException e) { e.printStackTrace(); } mCameraDevice.startPreview(); }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public void onResume() { addView(previewStrategy.getWidget()); if (camera == null) { cameraId = getHost().getCameraId(); if (cameraId >= 0) { try { camera = Camera.open(cameraId); if (getActivity().getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) { onOrientationChange.enable(); } setCameraDisplayOrientation(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && getHost() instanceof Camera.FaceDetectionListener) { camera.setFaceDetectionListener((Camera.FaceDetectionListener) getHost()); } } catch (Exception e) { getHost().onCameraFail(FailureReason.UNKNOWN); } } else { getHost().onCameraFail(FailureReason.NO_CAMERAS_REPORTED); } } }
public void record() throws Exception { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { throw new UnsupportedOperationException("Video recording supported only on API Level 11+"); } if (displayOrientation != 0 && displayOrientation != 180) { throw new UnsupportedOperationException("Video recording supported only in landscape"); } Camera.Parameters pictureParams = camera.getParameters(); setCameraPictureOrientation(pictureParams); camera.setParameters(pictureParams); stopPreview(); camera.unlock(); try { recorder = new MediaRecorder(); recorder.setCamera(camera); getHost().configureRecorderAudio(cameraId, recorder); recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); getHost().configureRecorderProfile(cameraId, recorder); getHost().configureRecorderOutput(cameraId, recorder); recorder.setOrientationHint(outputOrientation); previewStrategy.attach(recorder); recorder.prepare(); recorder.start(); } catch (IOException e) { recorder.release(); recorder = null; throw e; } }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override protected void onResume() { super.onResume(); Log.i(TAG, "onResume"); currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT; camera = Camera.open(currentCameraId); int pictureHeight = camera.getParameters().getPictureSize().height; int pictureWidth = camera.getParameters().getPictureSize().width; FrameLayout surfWrap = (FrameLayout) findViewById(R.id.surface_wrapper); surfWrap.setLayoutParams(new FrameLayout.LayoutParams(pictureHeight, pictureWidth)); Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); int orientation = display.getRotation(); Log.i("Rotation", String.valueOf(orientation)); if (orientation == Surface.ROTATION_90) { // TODO: add logic for landscape mode here Log.i("Rotation", "Landscape mode"); displayRotation = 0; } else if (orientation == Surface.ROTATION_270) { displayRotation = 270; } else { Log.i("Rotation", "Portrait mode"); displayRotation = 90; } }
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); mContext = CameraActivity.this; mUsersSQL = "select a.[CORPNO], a.[OPERNO], a.[OPERPASS] from b_opera a;"; mSearchUsersSQL = "select count(*) from b_opera a where a.[CORPNO] = ? and a.[OPERNO] = ?;"; mInsertOperaSQL = "INSERT INTO B_OPERA (CORPNO, OPERNO, OPERPASS) VALUES(?, ?, ?, ?, ?);"; mInsertDownInfoSQL = "INSERT INTO B_LOGININFO (CORPNO, PROJDESC, DOWNURL, UPLOADURL, DBFILENAME, DOWNTIME) VALUES(?, ?, ?, ?, ?, ?);"; mDownLoadInfoSQL = "select a.[CORPNO], a.[PROJDESC], a.[DOWNURL], a.[UPLOADURL], a.[DBFILENAME] from b_logininfo a;"; autoFocusHandler = new Handler(); mCamera = getCameraInstance(); /* Instance barcode scanner */ scanner = new ImageScanner(); scanner.setConfig(0, Config.X_DENSITY, 3); scanner.setConfig(0, Config.Y_DENSITY, 3); Log.i(TAG, "CameraActivity onCreate()"); mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB); FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview); preview.addView(mPreview); mCamera.setPreviewCallback(previewCb); mCamera.startPreview(); previewing = true; mCamera.autoFocus(autoFocusCB); // 初始化声音和震动 AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE); // 如果手机是震动模式就震动 if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) { playBeep = false; } // 初始化声音 initBeepSound(); initLoginDataBase(); // MyApp.showToast(CameraActivity.this, "hello"); Button mButtonBack = (Button) findViewById(R.id.button_back); /*返回按钮*/ mButtonBack.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("downloadfile", ""); setResult(RESULT_OK, intent); finish(); } }); }
public void flashOff(Context context) { if (isOn) { isOn = false; cam.stopPreview(); cam.release(); } }
/** * Sets the up camera to be connected to GPUImage to get a filtered preview. * * @param camera the camera * @param degrees by how many degrees the image should be rotated * @param flipHorizontal if the image should be flipped horizontally * @param flipVertical if the image should be flipped vertically */ public void setUpCamera( final Camera camera, final int degrees, final boolean flipHorizontal, final boolean flipVertical) { mGlSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) { setUpCameraGingerbread(camera); } else { camera.setPreviewCallback(mRenderer); camera.startPreview(); } Rotation rotation = Rotation.NORMAL; switch (degrees) { case 90: rotation = Rotation.ROTATION_90; break; case 180: rotation = Rotation.ROTATION_180; break; case 270: rotation = Rotation.ROTATION_270; break; } mRenderer.setRotationCamera(rotation, flipHorizontal, flipVertical); }
public int doZoom(float scale) { synchronized (mCameraLock) { if (mCamera == null) { return 0; } int currentZoom = 0; int maxZoom; Camera.Parameters parameters = mCamera.getParameters(); if (!parameters.isZoomSupported()) { Log.w(TAG, mContext.getString(R.string.zoom_not_supported)); return currentZoom; } maxZoom = parameters.getMaxZoom(); currentZoom = parameters.getZoom() + 1; float newZoom; if (scale > 1) { newZoom = currentZoom + scale * (maxZoom / 10); } else { newZoom = currentZoom * scale; } currentZoom = Math.round(newZoom) - 1; if (currentZoom < 0) { currentZoom = 0; } else if (currentZoom > maxZoom) { currentZoom = maxZoom; } parameters.setZoom(currentZoom); mCamera.setParameters(parameters); return currentZoom; } }
@SuppressWarnings("WrongConstant") private void setCameraDisplayOrientation(Camera.Parameters parameters) { Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(getCurrentCameraId(), info); final int deviceOrientation = Degrees.getDisplayRotation(getActivity()); mDisplayOrientation = Degrees.getDisplayOrientation( info.orientation, deviceOrientation, info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT); Log.d( "CameraFragment", String.format( "Orientations: Sensor = %d˚, Device = %d˚, Display = %d˚", info.orientation, deviceOrientation, mDisplayOrientation)); int previewOrientation; if (CameraUtil.isArcWelder()) { previewOrientation = 0; } else { previewOrientation = mDisplayOrientation; if (Degrees.isPortrait(deviceOrientation) && getCurrentCameraPosition() == CAMERA_POSITION_FRONT) previewOrientation = Degrees.mirror(mDisplayOrientation); } parameters.setRotation(previewOrientation); mCamera.setDisplayOrientation(previewOrientation); }
@Kroll.method @Kroll.getProperty public int[] getAvailableCameras() { int cameraCount = Camera.getNumberOfCameras(); if (cameraCount == 0) { return null; } int[] result = new int[cameraCount]; CameraInfo cameraInfo = new CameraInfo(); for (int i = 0; i < cameraCount; i++) { Camera.getCameraInfo(i, cameraInfo); switch (cameraInfo.facing) { case CameraInfo.CAMERA_FACING_FRONT: result[i] = CAMERA_FRONT; break; case CameraInfo.CAMERA_FACING_BACK: result[i] = CAMERA_REAR; break; default: // This would be odd. As of API level 17, // there are just the two options. result[i] = -1; } } return result; }
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // If your preview can change or rotate, take care of those events here. // Make sure to stop the preview before resizing or reformatting it. Log.i(TAG, "srface changed"); if (mHolder.getSurface() == null) { // preview surface does not exist return; } // stop preview before making changes try { mCamera.stopPreview(); } catch (Exception e) { // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or // reformatting changes here // start preview with new settings try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (Exception e) { Log.d(TAG, "Error starting camera preview: " + e.getMessage()); } }
public void surfaceDestroyed(SurfaceHolder holder) { if (mCameraDevice != null) { mCameraDevice.stopPreview(); mCameraDevice.release(); } mSurfaceHolder = null; }
public static void setCameraDisplayOrientation( Activity activity, int cameraId, android.hardware.Camera camera) { android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); android.hardware.Camera.getCameraInfo(cameraId, info); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; // compensate the mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360; } camera.setDisplayOrientation(result); }
/** 给摄像头设置好一系列相关参数,并开始预览 */ 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 updateDisplayOrientation(Context context) { if (!opened) throw new IllegalStateException("Camera not opened yet"); joinPreviewStarter(); final boolean restartPreview = previewing && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH; if (restartPreview) { previewing = false; camera.stopPreview(); } int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay() .getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } cameraRotation = (cameraOrientation - degrees + 360) % 360; camera.setDisplayOrientation(cameraRotation); if (restartPreview) { startPreview(); } }
/** * A single preview frame will be returned to the handler supplied. The data will arrive as byte[] * in the message.obj field, with width and height encoded as message.arg1 and message.arg2, * respectively. * * @param handler The handler to send the message to. * @param message The what field of the message to be sent. */ public synchronized void requestOcrDecode(Handler handler, int message) { Camera theCamera = camera; if (theCamera != null && previewing) { previewCallback.setHandler(handler, message); theCamera.setOneShotPreviewCallback(previewCallback); } }
@SuppressLint("NewApi") private void openCamera() { if (mCamera == null) { Log.d("", "Open new Camera Object"); if (android.os.Build.VERSION.SDK_INT >= 9) { try { Log.d("", "Trying to open camera with id = " + VinteractApplication.mCameraIDToOpen); mCamera = Camera.open(VinteractApplication.mCameraIDToOpen); } catch (RuntimeException RE) { RE.printStackTrace(); finishWithCameraRealease(); } } else { mCamera = Camera.open(); } if (mCamera == null) { Log.e("", "Error: Sorry your device does not have unused Camera."); finishWithCameraRealease(); return; } if (customVideoView != null) { Log.d("", "Set preview with new camera object onPause"); customVideoView.setCamera(mCamera); customVideoView.setPreviewCallback(this); } } }
private void startPreview() { if (mPausing || mMainActivity.isFinishing()) return; ensureCameraDevice(); // If we're previewing already, stop the preview first (this will blank // the screen). if (mPreviewing) stopPreview(); setPreviewDisplay(mSurfaceHolder); setCameraParameters(); // mCameraDevice.setPreviewCallback(mPreviewCallback); mCameraDevice.setPreviewCallback(cb); try { Log.v(TAG, "startPreview"); mCameraDevice.startPreview(); } catch (Throwable ex) { closeCamera(); throw new RuntimeException("startPreview failed", ex); } mPreviewing = true; mStatus = IDLE; }
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if (mHolder.getSurface() == null) { return; } // stop preview before making changes try { mCamera.stopPreview(); } catch (Exception e) { // ignore: tried to stop a non-existent preview } if (mSupportedPreviewSizes != null) { mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, w, h); } // start preview with new settings try { mCamera.setPreviewDisplay(mHolder); Parameters params = mCamera.getParameters(); // params.setPreviewSize(mPreviewSize.width, mPreviewSize.height); params.setPreviewSize( mSupportedPreviewSizes.get(0).width, mSupportedPreviewSizes.get(0).height); mCamera.setParameters(params); mCamera.startPreview(); Log.d(TAG, "Preview Size: " + mPreviewSize.width + "X" + mPreviewSize.height); } catch (Exception e) { Log.d(TAG, "Error starting camera preview: " + e.getMessage()); } }
public void offLight() { if (camera != null) { Camera.Parameters parameter = camera.getParameters(); parameter.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(parameter); } }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Hide the window title. requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setFormat(PixelFormat.UNKNOWN); setContentView(R.layout.main); // Create an instance of Camera mCamera = getCameraInstance(); Camera.Parameters mParameters = mCamera.getParameters(); mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO); // mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON); mCamera.setParameters(mParameters); // Create our Preview view and set it as the content of our activity. mPreview = new Preview(this, mCamera); FrameLayout preview = (FrameLayout) findViewById(R.id.preview); preview.addView(mPreview); // Create our Overlay mOverlay = new Overlay(this); preview.addView(mOverlay); mPreview.setOverlay(mOverlay); // Setting buttons buttonProc = (Button) findViewById(R.id.buttonProc); buttonBinImage = (Button) findViewById(R.id.buttonBinaryImage); buttonReset = (Button) findViewById(R.id.buttonReset); buttonProc.setOnClickListener(snapAndProcess()); buttonBinImage.setOnClickListener(switchBinaryImage()); buttonReset.setOnClickListener(reset()); }
/** Opens a rear-facing camera with {@link Camera#open(int)}, if one exists, or opens camera 0. */ public static Camera open() { int numCameras = Camera.getNumberOfCameras(); if (numCameras == 0) { Log.w(TAG, "No cameras!"); return null; } int index = 0; while (index < numCameras) { Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); Camera.getCameraInfo(index, cameraInfo); // CAMERA_FACING_BACK:手机背面的摄像头 if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { break; } index++; } Camera camera; if (index < numCameras) { Log.i(TAG, "Opening camera #" + index); camera = Camera.open(index); } else { Log.i(TAG, "No camera facing back; returning camera #0"); camera = Camera.open(0); } return camera; }
public void setFlashMode(int cameraType, int flashMode) { Camera camera = _cameras.get(cameraType); if (null == camera) { return; } Camera.Parameters parameters = camera.getParameters(); String value = parameters.getFlashMode(); switch (flashMode) { case RCTCameraModule.RCT_CAMERA_FLASH_MODE_AUTO: value = Camera.Parameters.FLASH_MODE_AUTO; break; case RCTCameraModule.RCT_CAMERA_FLASH_MODE_ON: value = Camera.Parameters.FLASH_MODE_ON; break; case RCTCameraModule.RCT_CAMERA_FLASH_MODE_OFF: value = Camera.Parameters.FLASH_MODE_OFF; break; } List<String> flashModes = parameters.getSupportedFlashModes(); if (flashModes != null && flashModes.contains(value)) { parameters.setFlashMode(value); camera.setParameters(parameters); } }