火炬应用程序安卓
你好,我想在 android 中实现 torch 应用程序,所以这是我的代码
setContentView(R.layout.main);
camera = Camera.open();
if(camera == null)
Toast.makeText(getBaseContext(),"CAMERA FAILED", Toast.LENGTH_LONG).show();
else
Toast.makeText(getBaseContext(),"camera opened",Toast.LENGTH_LONG).show();
}
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
if(camera!=null)
{
Toast.makeText(getBaseContext(),"came to volume up",Toast.LENGTH_LONG).show();
Parameters params = camera.getParameters();
params.setFlashMode( Parameters.FLASH_MODE_ON );
camera.setParameters(params);
}
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_UP) {
Parameters params = camera.getParameters();
params.setFlashMode( Parameters.FLASH_MODE_OFF );
camera.setParameters(params);
camera.release();
camera = null;
Toast.makeText(getBaseContext(),"came to volume down",Toast.LENGTH_LONG).show();
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
}
为什么这个代码不起作用???
hi i want to implement torch application in android so here is my code
setContentView(R.layout.main);
camera = Camera.open();
if(camera == null)
Toast.makeText(getBaseContext(),"CAMERA FAILED", Toast.LENGTH_LONG).show();
else
Toast.makeText(getBaseContext(),"camera opened",Toast.LENGTH_LONG).show();
}
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
if(camera!=null)
{
Toast.makeText(getBaseContext(),"came to volume up",Toast.LENGTH_LONG).show();
Parameters params = camera.getParameters();
params.setFlashMode( Parameters.FLASH_MODE_ON );
camera.setParameters(params);
}
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_UP) {
Parameters params = camera.getParameters();
params.setFlashMode( Parameters.FLASH_MODE_OFF );
camera.setParameters(params);
camera.release();
camera = null;
Toast.makeText(getBaseContext(),"came to volume down",Toast.LENGTH_LONG).show();
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
}
why this code is not working???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
杜尔加,
我相信你想要
FLASH_MODE_TORCH
< /a> 不是FLASH_MODE_ON
当您按音量增大键时。也许可能需要以下权限:
FLASHLIGHT
< /a> 和CAMERA
Durga,
I believe you want
FLASH_MODE_TORCH
notFLASH_MODE_ON
when you press the volume up key.Perhaps the following permissions may be required:
FLASHLIGHT
andCAMERA
对于三星,您需要有相机预览的表面视图才能使手电筒模式发挥作用。
你让它工作了吗?
For Samsung you're required to have a surface view for the camera preview inorder for the torch mode to work.
DID you get it to work yet?
有一个扩展 SurfaceView 并实现 SurfaceView.Callback 的类。在构造函数中,向持有者添加回调并将其类型设置为 PUSH_BUFFERS。
对于此类,在 surfaceCreated 方法中初始化您的 Camera 对象并在其中设置您的相机参数。
在surfaceDestroy中,释放相机对象。
此外,实施其他方法来确定三星设备是否有 LED 以及设备 LED 是否亮起。
在您的活动中,声明您的表面视图对象,将其添加到您的布局中,然后设置ContentView。
Have a class that extends SurfaceView and implements SurfaceView.Callback. In teh constructor, add a callback to the holder and set its type to PUSH_BUFFERS.
For this class, initialize your Camera object in the surfaceCreated method and set your camera parameters there.
In the surfaceDestroy, release camera object.
In addition, implement additional methods to determine if Samsung device have LED and device LED is lit.
In your activity, declare your surface view object add it to your layout, then setContentView.
William Tate 说得对,您需要 FLASH_MODE_TORCH 以及这两个权限,但在您开始视频预览之前,相机硬件不会被触及。
这段代码将打开火炬(尽管您可能想在实际应用程序中进行一些错误捕获):
并且格式正确的权限在您的 Android 清单中将如下所示:
William Tate was correct saying that you need FLASH_MODE_TORCH along with the two permissions, but the camera hardware doesn't get touched until you start a video preview.
This code will turn on the torch (though you may want to do some error catching in your actual app):
And the properly formatted permissions will look like this in your android manifest: