以下是我在Android中启用Flashlight的代码。在Toast消息中,它返回以下参数:auto,on,off,torch。将参数设置为FLASH_MODE_TORCH后,火炬不能启用。我的清单权限是:
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"/>
<uses-feature android:name="android.hardware.camera"></uses-feature>
我的代码:
public class CameraFlashActivity extends Activity {
/** Called when the activity is first created. */
public Camera mCamera;
Parameters cameraParameters;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
boolean cameraPresnt = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if( cameraPresnt == true)
{
Toast.makeText(getApplicationContext(),"Flash is there",10).show();
if(mCamera==null)
{
try {
mCamera = Camera.open();
mCamera.startPreview();
Toast.makeText(getApplicationContext(),"Camera is present",10).show();
} catch (Exception e) {
Log.i("CameraFlashActivity", "Camera.open() failed: " + e.getMessage());
}
if(mCamera==null)
{
Toast.makeText(getApplicationContext(),"Camera object is null",10).show();
}
else
{
cameraParameters = mCamera.getParameters();
if(cameraParameters==null)
{
Toast.makeText(getApplicationContext(),"No camera parameters",10).show();
}
else
{
List<String> flashmodes = cameraParameters.getSupportedFlashModes();
String supportedFlashModes = cameraParameters.getFlashMode();
Toast.makeText(getApplicationContext(),flashmodes.toString(),10).show();
Toast.makeText(getApplicationContext(),supportedFlashModes,10).show();
cameraParameters.setFlashMode(Parameters.FLASH_MODE_ON);
mCamera.setParameters(cameraParameters);
String supportFlashModes = cameraParameters.getFlashMode();
Toast.makeText(getApplicationContext(),supportFlashModes,10).show();
Toast.makeText(getApplicationContext(),"Camera parameters are set on flash light",10).show();
}
}
}
else
{
}
}
else
{
Toast.makeText(getApplicationContext(),"Flash is not there",10).show();
}
}
public void onDestroy() {
super.onDestroy();
if (mCamera != null) {
cameraParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(cameraParameters);
mCamera.stopPreview();
mCamera.release();
}
Log.i("cameraFlashLight", "onDestroy");
}
}
答案 0 :(得分:1)
使用以下代码序列:
mCamera = Camera.open();
cameraParameters = mCamera.getParameters();
cameraParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameters);
mCamera.startPreview();