在RK5.1的ROM上面用Android Camera2 找不到摄像头
在RK5.1的ROM上面用Android Camera2 的API 根本就找不到摄像头,当然也就不能打开了,USB摄像头,求官方的解决方案啊。。。用Camera是没有问题的 camera能打开usb摄像头,camera2肯定也能打开usb摄像头,有可能是你的代码问题。
你能不能把你camera2的代码发给我看看
我刚才用camera2测试一下,在RK5.1本版上可以打开usb摄像头。你是不是漏掉什么权限没有加
Activity中代码:
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private SurfaceView mSurfaceView;
private CameraManager mCameraManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
mSurfaceView = findViewById(R.id.surfaceview);
mSurfaceView.getHolder().addCallback(this);
}
@Override
public void surfaceCreated(final SurfaceHolder holder) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
}
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//在打开摄像头时,你先查看一下设备上摄像头id列表:有可能只有一个,"1"的值:根据的你情况而定
mCameraManager.openCamera("1", new CameraDevice.StateCallback() {
@Override
public void onOpened(final CameraDevice camera) {
try {
// 创建预览需要的CaptureRequest.Builder
final CaptureRequest.Builder previewRequestBuilder =
camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
// 将mSurfaceView的surface作为CaptureRequest.Builder的目标
previewRequestBuilder.addTarget(holder.getSurface());
//如果holder.getSurface().isValid()为false,则会出错;因为你的画布无效导致的问题
if (holder.getSurface().isValid())
//创建相机捕获会话,用于预览的工作。
camera.createCaptureSession(Arrays.asList(holder.getSurface()), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession cameraCaptureSession) {
if (null == camera) {
return;
}
try {
//设置预览模式
previewRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
// 自动对焦
previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// 打开闪光灯
previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
//重复请求预览
cameraCaptureSession.setRepeatingRequest(previewRequestBuilder.build(), null, new Handler());
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
}
}, new Handler());
} catch (CameraAccessException pE) {
Log.e(TAG,pE.getMessage());
pE.printStackTrace();
}
}
@Override
public void onDisconnected(CameraDevice camera) {
camera.close();
}
@Override
public void onError(CameraDevice camera, int error) {
camera.close();
}
}, new Handler());
}
} catch (CameraAccessException pE) {
pE.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
layout中源码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.firefly.camera2.MainActivity">
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
mainfest.xml中的源码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.firefly.camera2">
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest> USB Camera不支持热插拔,Camera service在系统启动的时候遍历了Camera,系统起来后你插入USB Camera就遍历不到了。可以通过命令stop media; start media来从新遍历,然后app就可以打开USB Camera了。 插拔camera看看打印信息,设备节点是否有生成?
页:
[1]