在阅读了其他有同样问题的用户的所有帖子后,我能够创建一个简单的工作应用程序来打开我的 Nexus 5 上的闪光灯,这是“OnCreate()”方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Camera mCamera;
SurfaceView preview;
mCamera = Camera.open();
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
try {
mCamera.setPreviewTexture(new SurfaceTexture(0));
} catch (IOException e) {
e.printStackTrace();
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
在 list 中,我添加了以下权限:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-feature android:name="android.hardware.camera.flash" android:required="false" />
我需要将它放在一个更复杂的应用程序中,该应用程序也使用相机。我如何将它添加为方法/类或其他东西,以便它可以在没有冲突的情况下工作?
谢谢
最佳答案
完整编辑:
根据您的评论,我了解您希望在使用相机时使用手电筒闪光模式。这是可能的,下面是一些纯粹的概念验证代码。请注意,它实现了非常少的异常处理,并且需要进行一些调整才能根据您的需要工作,但它将演示帮助您入门的基础知识。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
以启动相机预览。之后,您可以使用 Toggle 按钮打开和关闭 Torch,然后单击 Picture
拍摄将保存到外部存储目录根目录的照片。在退出或最小化应用程序之前,您应该单击 Done
。编辑: 最小
向 list 添加以下权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
将以下内容添加到您的 Activity 中:
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();
}
关于android - Google Nexus 5 上的闪光灯,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22630453/