Android 相机 LED 灯在 2 秒后熄灭?

发布于 2024-12-15 08:14:18 字数 832 浏览 1 评论 0原文

我在我的应用程序中使用相机闪光灯,我已经完成了编码,它正在打开/关闭灯。 但 2 秒后它就会关闭。如果我再次按下打开按钮,它就会强制关闭。 这是我正在使用的代码,请帮助我。

我想要这样,如果用户按下打开按钮灯亮起,直到用户按下关闭按钮。

private void processOffClick()  {

    //togglebutton.setButtonDrawable(R.drawable.offbutton);
    System.out.println("in off state");
    if( cam != null ){
        cam.stopPreview();
        cam.release();
    }
}
private void processOnClick()  {

    //togglebutton.setButtonDrawable(R.drawable.onbutton);
    System.out.println("in on state");       
    cam = Camera.open();     
    Parameters params = cam.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_ON);
    cam.setParameters(params);

    cam.startPreview();
    cam.autoFocus(new AutoFocusCallback() {
        public void onAutoFocus(boolean success, Camera camera) {
        }
    });      
}

I am using the camera flash light in my application, I was done coding for that, it's working on/off the light.
but after 2 seconds it goes to off. If I press the on button again it was giving force close.
This is the code i am using for this, please help me.

I want this like if user presses the on button light On, upto user press Off button.

private void processOffClick()  {

    //togglebutton.setButtonDrawable(R.drawable.offbutton);
    System.out.println("in off state");
    if( cam != null ){
        cam.stopPreview();
        cam.release();
    }
}
private void processOnClick()  {

    //togglebutton.setButtonDrawable(R.drawable.onbutton);
    System.out.println("in on state");       
    cam = Camera.open();     
    Parameters params = cam.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_ON);
    cam.setParameters(params);

    cam.startPreview();
    cam.autoFocus(new AutoFocusCallback() {
        public void onAutoFocus(boolean success, Camera camera) {
        }
    });      
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花细雨 2024-12-22 08:14:18

将以下行:

Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
cam.setParameters(params);

放在 processOffClick 中,而不是将其放在 processOnClick 中
像这样:

boolean clicked = false;
private void processOffClick()  {

  //togglebutton.setButtonDrawable(R.drawable.offbutton);
  clicked = false;
  System.out.println("in off state");
  if( cam != null ){
      cam.stopPreview();
      cam.release();
  }
}
private void processOnClick()  {
  clicked = true;
  //togglebutton.setButtonDrawable(R.drawable.onbutton);
  System.out.println("in on state");       
  cam = Camera.open();     
  Parameters params = cam.getParameters();
  params.setFlashMode(Parameters.FLASH_MODE_ON);

  while(clicked)  { 
     cam.setParameters(params);

     cam.startPreview();
        cam.autoFocus(new AutoFocusCallback() {
           public void onAutoFocus(boolean success, Camera camera) {
        }
     });
  } 
}

它可能会起作用,我没有检查代码

,我添加了一个 while 循环,这样它就会保持闪光灯和焦点,直到它未被点击。

put the lines:

Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
cam.setParameters(params);

in processOffClick instead of putting it in processOnClick
like that:

boolean clicked = false;
private void processOffClick()  {

  //togglebutton.setButtonDrawable(R.drawable.offbutton);
  clicked = false;
  System.out.println("in off state");
  if( cam != null ){
      cam.stopPreview();
      cam.release();
  }
}
private void processOnClick()  {
  clicked = true;
  //togglebutton.setButtonDrawable(R.drawable.onbutton);
  System.out.println("in on state");       
  cam = Camera.open();     
  Parameters params = cam.getParameters();
  params.setFlashMode(Parameters.FLASH_MODE_ON);

  while(clicked)  { 
     cam.setParameters(params);

     cam.startPreview();
        cam.autoFocus(new AutoFocusCallback() {
           public void onAutoFocus(boolean success, Camera camera) {
        }
     });
  } 
}

It might work, i didn't check the code

I added a while loop so it would hold the flash and the focus until its unclicked.

回忆追雨的时光 2024-12-22 08:14:18

我的经验表明,闪光模式应该是“TORCH”(如果支持),并且仅当您开始预览时才会启动。然而,相机在不同设备上的表现非常不同,并不总是像其功能描述中所宣传的那样

My experience says, that flash mode ought to be "TORCH" ( if supported ) and it is started only when you start preview. However, cameras behave very differently on different devices and not always as advertised in their capabilities descrioptors

俯瞰星空 2024-12-22 08:14:18

1.打开

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();

2.关闭

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();

然后,在 AndroidManifest.xml 上添加以下权限。

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

看到这个 http://www.mkyong .com/android/how-to-turn-onoff-camera-ledflashlight-in-android/

1.Turn on

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();

2. Turn off

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();

And, put following permission on AndroidManifest.xml.

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

see this one http://www.mkyong.com/android/how-to-turn-onoff-camera-ledflashlight-in-android/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文