完整编辑:
根据您的评论,我了解到您希望在使用相机时使用手电筒闪光模式。这是可能的,下面是一些纯粹的概念验证代码。请注意,它实现了非常少的异常处理,并且需要进行一些调整才能按您的需要工作,但它会展示让您入门的基础知识。
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<android.view.SurfaceView android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="initialize"
android:text="Init" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="takePicture"
android:text="Picture" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="release"
android:text="Done" />
<ToggleButton android:id="@+id/tgbToggle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
Camera camera;
Parameters params;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
PictureCallback callBackJpeg;
Button start, stop, capture;
ToggleButton toggle;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton) findViewById(R.id.tgbToggle);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean checked)
{
toggleTorch(checked);
}
}
);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
callBackJpeg = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera)
{
FileOutputStream fos = null;
String filename = String.format(Environment.getExternalStorageDirectory().toString() +
"/%d.jpg", System.currentTimeMillis());
try
{
fos = new FileOutputStream(filename);
fos.write(data);
fos.close();
Toast.makeText(MainActivity.this, "Picture saved - " + filename, 0).show();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
};
}
public void initialize(View v)
{
camera = Camera.open();
params = camera.getParameters();
try
{
camera.setPreviewDisplay(surfaceHolder);
}
catch (IOException e)
{
Toast.makeText(this, "Unable to set preview display.", 0).show();
return;
}
camera.startPreview();
}
public void takePicture(View v)
{
camera.takePicture(null, null, callBackJpeg);
}
public void release(View v)
{
camera.stopPreview();
camera.release();
}
private void toggleTorch(boolean turnOn)
{
params.setFlashMode(turnOn ? Parameters.FLASH_MODE_TORCH : Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.startPreview();
}
}
当应用程序启动时,您需要单击Init
以启动相机预览。在此之后,您可以使用切换按钮打开和关闭 Torch,然后单击Picture
以拍摄将保存到外部存储目录根目录的照片。您应该Done
在退出或最小化应用程序之前单击。
编辑:最小
将以下权限添加到清单:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
将以下内容添加到您的活动中:
Camera camera;
Parameters params;
@Override
public void onResume()
{
super.onResume();
camera = Camera.open();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
}
@Override
public void onPause()
{
super.onPause();
camera.release();
}