相机Flash在Android中不起作用

时间:2016-09-02 05:47:45

标签: android android-studio

我打电话给以下"行动方法"点击按钮点击按钮,当我点击按钮闪光灯无法正常工作时帮助我。

private void  actionFlash(){

/* First check if device is supporting flashlight or not */
    hasFlash = this.getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    if(!hasFlash){
        // device doesn't support flash
        // Show alert message and close the application
        AlertDialog alert = new AlertDialog.Builder(this).create();
        alert.setMessage("Sorry, your device doesn't support flash light!");
        alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.dismiss();
            }
        });
        alert.show();
        return;
    }
    else{  
        CameraManager mCameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
        try{
            String mCameraId = mCameraManager.getCameraIdList()[0];
            if(mCameraId.equals("1")) {    // currently on back camera
                if(!isFlashOn){  // if flash light was OFF
                    // Turn ON flash light
                    try{
                        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                            mCameraManager.setTorchMode(mCameraId, true);
                        }
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    // Change isFlashOn boolean value
                    isFlashOn = true;
                    // Change button icon
                    flash.setImageResource(R.drawable.ic_flash_off_black_24dp);

                }else{ // if flash light was ON
                    // Turn OFF flash light
                    try{
                        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                            mCameraManager.setTorchMode(mCameraId, false);
                        }
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    // Change isFlashOn boolean value
                    isFlashOn = false;
                    // Change button icon
                    flash.setImageResource(R.drawable.ic_flash_on_black_24dp);
                }
            }
        }catch(CameraAccessException e){
            Toast.makeText(this, "Cannot access the camera.", Toast.LENGTH_SHORT).show();
            this.finish();
        }
    }
}

我使用以下设置相机代码,闪光按钮甚至没有响应:

private void setupCamera(int width, int height) {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for(String cameraId : cameraManager.getCameraIdList()){
            CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
            if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) ==
                    CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
                }
                StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                int deviceOrientation = getWindowManager().getDefaultDisplay().getRotation();
                mTotalRotation = sensorToDeviceRotation(cameraCharacteristics, deviceOrientation);
                boolean swapRotation = mTotalRotation == 90 || mTotalRotation == 270;
                int rotatedWidth = width;
                int rotatedHeight = height;
                if(swapRotation) {
                    rotatedWidth = height;
                    rotatedHeight = width;
                }
                mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedWidth, rotatedHeight);
                mVideoSize = chooseOptimalSize(map.getOutputSizes(MediaRecorder.class), rotatedWidth, rotatedHeight);
                mImageSize = chooseOptimalSize(map.getOutputSizes(ImageFormat.JPEG), rotatedWidth, rotatedHeight);
                mImageReader = ImageReader.newInstance(mImageSize.getWidth(), mImageSize.getHeight(), ImageFormat.JPEG, 1);
                mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);
                mCameraId = cameraId;
                return;
            }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案