使用android.hardware.camera2访问摄像头
我想创建一个应用程序,允许我控制相机上的闪光灯,但是我对如何实现这一点的研究已经导致了大量使用最近贬值的android.hardware.cameraapi的例子。有人能告诉我如何使用android.hardware.camera2制作一个简单的手电筒应用程序的正确方向吗?你应该研究一下这个示例。这里展示了一种使用使用android.hardware.camera2访问摄像头,android,android-camera,android-camera-intent,flashlight,android-hardware,Android,Android Camera,Android Camera Intent,Flashlight,Android Hardware,我想创建一个应用程序,允许我控制相机上的闪光灯,但是我对如何实现这一点的研究已经导致了大量使用最近贬值的android.hardware.cameraapi的例子。有人能告诉我如何使用android.hardware.camera2制作一个简单的手电筒应用程序的正确方向吗?你应该研究一下这个示例。这里展示了一种使用 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
我一直在研究一种方法,让预览纹理中显示的所有帧进行一些图像处理,如果你找到一种方法,请让我知道=D。android.hardware.camera不受欢迎。但你仍然可以使用它,它在棒棒糖和预棒棒糖中工作
几天前我就试过了如果你只想做一个简单的手电筒应用程序,你应该编辑你的问题。 将摄像头与新的android.hardware.camera2配合使用更为复杂 我曾尝试使用android studio和nexus lg 5x制作相机预览应用程序,但没有成功 相反,如果您只想打开闪光灯,以下是代码:
void torch(){
/* turn on the flash light */
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String[] cameraID = new String[]{};
cameraID = cameraManager.getCameraIdList();
for(int i = 0; i<cameraID.length; i++){
Log.e(TAG,cameraID[i]);
}
/* camera id is 0 and 1. 0 is the back camera, 1 is the front camera */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager.setTorchMode(cameraID[0],true);
//true means turned on, false, means turned off.
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
void torch(){
/*打开闪光灯*/
CameraManager CameraManager=(CameraManager)getSystemService(Context.CAMERA_服务);
试一试{
字符串[]cameraID=新字符串[]{};
cameraID=cameraManager.getCameraIdList();
对于(int i=0;i=Build.VERSION\u CODES.M){
setorchmode(cameraManager[0],true);
//true表示打开,false表示关闭。
}
}捕获(CameraAccessE异常){
e、 printStackTrace();
}
}
舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera2.full" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/MaterialTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
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>
MainActivity.java:
package com.example.test;
import android.app.Activity;
import android.graphics.Camera;
import android.hardware.camera2.params.InputConfiguration;
import android.os.Build;
import android.os.Bundle;
public class MainActivity extends Activity {
private final static String TAG = "Camera2testJ";
private Size mPreviewSize;
private TextureView mTextureView;
private CameraDevice mCameraDevice;
private CaptureRequest.Builder mPreviewBuilder;
private CameraCaptureSession mPreviewSession;
private Button mBtnShot;
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mTextureView = (TextureView)findViewById(R.id.texture);
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
mBtnShot = (Button)findViewById(R.id.btn_takepicture);
mBtnShot.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Log.e(TAG, "mBtnShot clicked");
torch();
}
});
}
void torch(){
/* try the flash light */
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);;
try {
String[] cameraID = new String[]{};
cameraID = cameraManager.getCameraIdList();
for(int i = 0; i<cameraID.length; i++){
Log.e(TAG,cameraID[i]);
}
/* camera id is 0 and 1. which one is the front or the back camera? */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager.setTorchMode(cameraID[0],true);
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
package com.example.test;
导入android.app.Activity;
导入android.graphics.Camera;
导入android.hardware.camera2.params.InputConfiguration;
导入android.os.Build;
导入android.os.Bundle;
公共类MainActivity扩展了活动{
私有最终静态字符串TAG=“Camera2testJ”;
私人规模的mPreviewSize;
私有纹理视图mTextureView;
私人摄像设备;
私人CaptureRequest.Builder mPreviewBuilder;
私人摄像机拍摄会议回顾;
私人按钮;
专用静态最终SparseIntArray方向=新SparseIntArray();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_全屏);
setContentView(R.layout.activity_main);
mTextureView=(TextureView)findViewById(R.id.texture);
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
mBtnShot=(按钮)findviewbyd(R.id.btn_takepicture);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
Log.e(标记“mBtnShot clicked”);
火炬();
}
});
}
无效火炬(){
/*试试闪光灯*/
CameraManager CameraManager=(CameraManager)getSystemService(Context.CAMERA_SERVICE);;
试一试{
字符串[]cameraID=新字符串[]{};
cameraID=cameraManager.getCameraIdList();
对于(int i=0;i=Build.VERSION\u CODES.M){
setorchmode(cameraManager[0],true);
}
}捕获(CameraAccessE异常){
e、 printStackTrace();
}
}
布局活动_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/texture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/btn_takepicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/picture"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
this?在询问之前使用搜索如果您想使用此答案,请不要包含这些内容。此权限不存在,并且您的应用程序不再适用于任何设备。