java怎么调用手机手电_java – 如何在android中同时打开手电筒和前置摄像头

本文档介绍了一个Android应用的需求,该应用需要打开前置摄像头预览,并同时开启手电筒。作者提供了相关代码实现,但在实际运行中发现无法同时打开前置摄像头和手电筒。代码中包含了相机的初始化、打开、关闭以及手电筒的开关控制。目前遇到的问题是当使用前置摄像头时,无法正常开启手电筒。
摘要由CSDN通过智能技术生成

在我的应用程序的一个要求中,我需要弹出一个包含前置摄像头预览的活动,同时我也需要打开手电筒.但是我观察到,我可以打开手电筒并返回相机,但没有前置摄像头和手电筒在一起.以下是我的代码:

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);

}

}

}

表现

如何同时打开手电筒和前凸轮?任何帮助,将不胜感激 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值