如何在android中同时打开手电筒和前置摄像头

时间:2022-12-01
本文介绍了如何在android中同时打开手电筒和前置摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序的一个要求中,我需要弹出一个包含前置摄像头预览的活动,同时我还需要打开手电筒.但是我观察到,我能够打开手电筒和后置摄像头,但不是前置摄像头和手电筒.以下是我的代码:

In one of the requirement in my app I need to pop up an activity containing the front camera preview,at this same time I need to turn on the flashlight as well.However I observe that,I am able to turn on the flashlight and back camera but not front camera and flashlight together.Following is my code:

    public class Cam extends Activity {

        private static int cameraId = 0;
        private Camera camera;

        //Adding for camera preview
        public static FrameLayout preview;
        public static CameraPreview mPreview;
        Context context;

        ImageButton btnSwitch;
        private boolean isFlashOn;
        private boolean hasFlash;
        Parameters params;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Log.e("Cam","Inside onCreate");
            setContentView(R.layout.cam);
            context = getApplicationContext();      

            btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);

            hasFlash = getApplicationContext().getPackageManager()
                    .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

            startCamera();

            // displaying button image
            toggleButtonImage();        

            btnSwitch.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (isFlashOn) {
                        turnOffFlash();
                    } else {
                        turnOnFlash();
                    }
                }
            });     
        }

        @Override
        protected void onPause() {
            super.onPause();

            turnOffFlash();

            Log.e("Cam","Inside onPause");
            try {
                if (camera != null) {
                    camera.release();
                    camera = null;
                    preview.removeView(mPreview);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void onResume() {
            super.onResume();

            Log.e("Cam","Inside onResume");
            try {
                if(camera ==null) {
                    Log.d("Cam","in resume,camera is  null");
                    try {
                        camera = android.hardware.Camera.open(cameraId); //opens front cam              
                        // camera = Camera.open(); when I use this I can on/off the flashlight,since I am using the back camera.
                        mPreview = new CameraPreview(this, camera);
                        preview = (FrameLayout) findViewById(R.id.camera_preview);
                        preview.addView(mPreview);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.e("Cam","Inside onDestroy");
            if (camera != null) {
                try {
                    camera.release();
                    camera = null;
                    preview.removeView(mPreview);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        private void startCamera() {

                    Log.e("Cam","Inside doInBackground");
                    String msg = "";
                    // Do we have a camera?
                    try {
                        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

                        } else {
                            cameraId = AppFunctions.findFrontFacingCamera();//A function that returns 0 for front camera
                            if (cameraId < 0) {

                            } else {

                                try {
                                    camera = Camera.open(cameraId);//opens front cam 
                                    // camera = Camera.open(); when I use this I can on/off the flashlight,since I am calling the back camera.
                                    params = camera.getParameters(); 
                                    Log.e("Cam","camera id" + cameraId);
                                    Log.e("Cam","params" + params);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                                try {
                                    mPreview = new CameraPreview(getApplicationContext(), camera);
                                    preview = (FrameLayout) findViewById(R.id.camera_preview);
                                    preview.addView(mPreview);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    } catch (Exception e3) {
                        e3.printStackTrace();
                    }
        }

        private void turnOnFlash() {
            Log.v("Cam","Inside turnOnFlash");
            if (!isFlashOn) {
                if (camera == null || params == null) {
                    Log.v("Cam","camera or param is null");
                    return;
                }

                params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(params);
                camera.startPreview();
                isFlashOn = true;

                // changing button/switch image
                toggleButtonImage();
            }

        }

        /*
         * Turning Off flash
         */
        private void turnOffFlash() {
            Log.v("Cam","Inside turnOffFlash");
            if (isFlashOn) {
                if (camera == null || params == null) {
                    Log.v("Cam","camera or param is null");
                    return;
                }
                params.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(params);
                camera.stopPreview();
                isFlashOn = false;

                // changing button/switch image
                toggleButtonImage();
            }
        }

        private void toggleButtonImage(){
            if(isFlashOn){
                btnSwitch.setImageResource(R.drawable.btn_switch_on);
            }else{
                btnSwitch.setImageResource(R.drawable.btn_switch_off);
            }
        }
    }

清单

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.front" />
<permission android:name="android.permission.FLASHLIGHT" />  

如何同时打开手电筒和前置摄像头?任何帮助将不胜感激!

How can I turn on the flashlight and front cam simultaneously? Any help would be appreciated !

推荐答案

手电筒

 public void onToggleClicked(View view) {
    PackageManager pm = context.getPackageManager();
    final Parameters p = camera.getParameters();
    if (isFlashSupported(pm)) {
        boolean on = ((ToggleButton) view).isChecked();
        if (on) {
            Log.i("info", "torch is turn on!");
            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(p);
            camera.startPreview();
        } else {
            Log.i("info", "torch is turn off!");
            p.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(p);
            camera.stopPreview();
        }

相机:

 ImageButton ib = (ImageButton) findViewById(R.id.buttonToast);
    ib.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, CAPTURE_IMAGE_CAPTURE_CODE);
        }
    });

这篇关于如何在android中同时打开手电筒和前置摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:BitmapFactory.decodeByteArray() 返回 NULL 下一篇:Android ACTION_IMAGE_CAPTURE 有时不调用 onActivityResult

相关文章

最新文章