在我的应用程序中,我有一个定制相机的片段,我正在尝试为我的相机实现一个flash开/关按钮。
现在我有这样的事情:
private Camera camera;
private Camera.Parameters params;
private boolean flash;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// ...
// Flash on/off button click;
final ImageButton buttonTurnFlash = (ImageButton) view.findViewById(R.id.button_turn_flash);
buttonTurnFlash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { turnFlash(); }
});
}
private void turnFlash() {
if (flash) flash = false;
else flash = true;
}然后,当用户单击“拍摄照片”按钮时,我调用此方法:
private void takePicture() {
try {
if (flash) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
}
final Bitmap bitmap = textureView.getBitmap();
final FileOutputStream fos = Controller.getContext().openFileOutput("image.jpg", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) { e.printStackTrace(); }
}如果闪光灯关闭了,我会得到一张非常好的照片。然而,如果我打开闪光灯,试着拍一张照片,闪光灯确实闪烁,但结果是一张没有闪光灯的照片,就像闪光灯关闭了一样。
我在相机方面非常新手,但我很确定当闪光灯闪烁,然后调用方法getBitmap()时,当他得到位图时,闪光灯已经消失或已经眨眼了。是否有任何方法将闪存延迟到getBitmap()方法完成为止?做这个最好的方法是什么?
发布于 2016-09-26 11:23:22
若要使用闪存模式拍照,请使用params.setFlashMode(Parameters.FLASH_MODE_ON)
发布于 2016-09-26 11:32:52
private boolean flashmode = false;
// check the flash is available or not by writing this code in onCreate
if (!getBaseContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
buttonTurnFlash.setVisibility(View.GONE);
}并在按钮的flashOnButton onClick中调用onClick()方法。
private void flashOnButton() {
if (camera != null) {
try {
Camera.Parameters param = camera.getParameters();
param.setFlashMode(!flashmode ? Camera.Parameters.FLASH_MODE_TORCH
: Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(param);
flashmode = !flashmode;
} catch (Exception e) {
`enter code here` // TODO: handle exception
}
}
}https://stackoverflow.com/questions/39701422
复制相似问题