如何在三星 Galaxy Tab 上使用相机闪光灯/led 作为手电筒? [英] How to use camera flash/led as torch on a Samsung Galaxy Tab?
问题描述
我遇到了三星 Galaxy Tab 的问题.我想将相机闪光灯用作手电筒.
I'm facing to a problem with a Samsung Galaxy Tab. I want to use the camera flash as torch.
有谁知道如何启用它?
在此提供的代码可用于在 HTC Desire 上启用/禁用相机闪光灯,但在三星 Galaxy Tab 上失败.
Hereby a code that works to enable/disable the camera flash on a HTC Desire but fails on Samsung Galaxy Tab.
FlashLight.java :
package com.example.FlashLight;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FlashLight extends Activity {
private final static String LOG_TAG = "FlashLight";
private Button mOnBtn;
private Button mOffBtn;
private Camera mCamera;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mOnBtn = (Button) findViewById(R.id.on_btn);
mOnBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
processOnClick();
}
});
mOffBtn = (Button) findViewById(R.id.off_btn);
mOffBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
processOffClick();
}
});
}
@Override
protected void onResume() {
super.onResume();
try{
mCamera = Camera.open();
} catch( Exception e ){
Log.e(LOG_TAG, "Impossible d'ouvrir la camera");
}
}
@Override
protected void onPause() {
if( mCamera != null ){
mCamera.release();
mCamera = null;
}
super.onPause();
}
private void processOffClick(){
if( mCamera != null ){
Parameters params = mCamera.getParameters();
params.setFlashMode( Parameters.FLASH_MODE_OFF );
mCamera.setParameters( params );
}
}
private void processOnClick(){
if( mCamera != null ){
Parameters params = mCamera.getParameters();
params.setFlashMode( Parameters.FLASH_MODE_TORCH );
mCamera.setParameters( params );
}
}
}
AndroidManifest.xml:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".FlashLight"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
</manifest>
layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/on_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Flash ON" />
<Button
android:id="@+id/off_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Flash OFF" />
</LinearLayout>
谢谢!
推荐答案
你没有做错任何事.事实上,你所做的一切都是正确的.您遇到了一个在 Android 世界中非常普遍的特定于设备的问题.我发现了 FLASH_MODE_TORCH 的以下行为模式:
You aren't doing anything wrong. In fact, you are doing everything correct. You are encountering a Device-Specific issue that is very prevalent in the Android world. I have found the following behavioral patterns for FLASH_MODE_TORCH:
- 在所有情况下都能正常工作
- 工作正常,但不能自动对焦
- 根本不起作用
令人沮丧的是,getSupportedFlashModes() 将在几乎所有设备上返回 FLASH_MODE_TORCH,而实际上只有少数设备支持它.
Frustratingly, getSupportedFlashModes() will return FLASH_MODE_TORCH on nearly every device when only a handful actually support it.
此外,某些设备实现会调整支持的闪存模式.如果您通过 Camera.Parameters 您可以尝试设置将闪存模式设置为 FLASH_MODE_ON、FLASH_MODE_AUTO 或 FLASH_MODE_RED_EYE 并查看它们中的任何一个是否有效.注意 - 这是一个特定于设备的 hack.
Also, some device implementations swizzle the supported flash modes. If you go through Camera.Parameters you can try setting the flash mode to FLASH_MODE_ON, FLASH_MODE_AUTO or FLASH_MODE_RED_EYE and see whether any of them work. Note - this is a device-specific hack.
我已向 Google 提交了这些类型的错误关于 DroidX 和 Nexus S.他们将其作为特定于设备的问题关闭.我想说向三星报告此问题,希望得到驱动程序或固件修复,但他们的 Android 支持渠道 不存在.
I have filed these types of bugs with Google regarding the DroidX and Nexus S. They closed it as a device-specific issue. I would say to report this to Samsung in hopes for a driver or firmware fix, but their Android support channels do not exist.
这篇关于如何在三星 Galaxy Tab 上使用相机闪光灯/led 作为手电筒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!