基于android的 rk3399 同时支持多个USB摄像头

基于android的 rk3399 同时支持多个USB摄像头

一、前文

复制代码
Android系统默认支持2个摄像头,一个前置摄像头,一个后置摄像头
需要支持数量更多的摄像头,得修改Android Hal层的代码

在这里插入图片描述

二、CameraHal_Module.h

复制代码
修改CAMERAS_SUPPORT_MAX

三、CameraHal_Module.cpp

复制代码
修改camera_get_number_of_cameras()函数中变量camInfoTmp[]相关代码

四、编译&烧录Image

该部分的修改要生效的话,需要进行全编译

复制代码
    ./build.sh
    ./build.sh mkimage
    ./build.sh mkupdate

五、App验证

AndroidManifest.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-featureandroid:name="android.hardware.camera"android:required="false" />
<uses-featureandroid:name="android.hardware.camera.autofocus"android:required="false" />
<applicationandroid:allowBackup="true"
							android:dataExtractionRules="@xml/data_extraction_rules"
							android:fullBackupContent="@xml/backup_rules"
							android:icon="@mipmap/ic_launcher"
							android:label="@string/app_name"
							android:roundIcon="@mipmap/ic_launcher_round"
							android:supportsRtl="true"
							android:theme="@style/Theme.CameraApplication"tools:targetApi="31">
	<activityandroid:name=".MainActivity"
						android:exported="true">
						<intent-filter><action android:name="android.intent.action.MAIN" />
						<category android:name="android.intent.category.LAUNCHER" />
						</intent-filter><meta-dataandroid:name="android.app.lib_name"android:value="" />
	</activity>
</application>
</manifest>

MainActivity.java

复制代码
package com.example.cameraapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
Camera camera1, camera2, camera3, camera4;
SurfaceHolder surfaceHolder1, surfaceHolder2, surfaceHolder3, surfaceHolder4;
SurfaceView surfaceView1, surfaceView2, surfaceView3, surfaceView4;
List<Camera> cameraList = new ArrayList<>();
List<SurfaceHolder> surfaceHolderList = new ArrayList<>();
List<SurfaceView> surfaceViewList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    surfaceView1 = findViewById(R.id.camera_surface_view1);
    surfaceView2 = findViewById(R.id.camera_surface_view2);
    surfaceView3 = findViewById(R.id.camera_surface_view3);
    surfaceView4 = findViewById(R.id.camera_surface_view4);
    cameraList.add(camera1);
    cameraList.add(camera2);
    cameraList.add(camera3);
    cameraList.add(camera4);
    surfaceHolderList.add(surfaceHolder1);
    surfaceHolderList.add(surfaceHolder2);
    surfaceHolderList.add(surfaceHolder3);
    surfaceHolderList.add(surfaceHolder4);
    surfaceViewList.add(surfaceView1);
    surfaceViewList.add(surfaceView2);
    surfaceViewList.add(surfaceView3);
    surfaceViewList.add(surfaceView4);
    for(int i=0; i<cameraList.size(); i++){
        initCamera(i, cameraList.get(i), surfaceHolderList.get(i), surfaceViewList.get(i));
    }
    int number = Camera.getNumberOfCameras();//得到摄像头的个数
    Toast.makeText(MainActivity.this, "摄像头个数:"+number, Toast.LENGTH_LONG).show();
}
private void initCamera(int number, Camera camera, SurfaceHolder surfaceHolder, SurfaceView surfaceView){
    try{
        camera = Camera.open(number);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        final Camera mCamera = camera;
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                try {
                    if (mCamera!=null) {
                        mCamera.setPreviewDisplay(surfaceHolder);
                        mCamera.setDisplayOrientation(90);mCamera.startPreview();
                        }
                        } 
                        catch (Exception e) {
                            e.printStackTrace();
                            }
                            }
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

            }
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                releaseCamera(mCamera);
                }
        });
    }catch (Exception e){
        e.printStackTrace();
    }
}
/*** 释放相机资源*/
private void releaseCamera(Camera camera) {
    if (camera != null) {
        camera.setPreviewCallback(null);
        camera.stopPreview();
        camera.release();
        camera = null;
        }
}
/*** 释放相机资源*/
private void releaseAllCamera() {
    for(int i=0; i<cameraList.size(); i++){
        if (cameraList.get(i) != null) {
            cameraList.get(i).setPreviewCallback(null);
            cameraList.get(i).stopPreview();
            cameraList.get(i).release();
            cameraList.set(i, null);
        }
    }
}
}

activity_main.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--    <TextView-->
<!--        android:layout_gravity="center"-->
<!--        android:layout_margin="10dp"-->
<!--        android:layout_width="wrap_content"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:text="摄像头个数:"-->
<!--        app:layout_constraintBottom_toBottomOf="parent"-->
<!--        app:layout_constraintEnd_toEndOf="parent"-->
<!--        app:layout_constraintStart_toStartOf="parent"-->
<!--        app:layout_constraintTop_toTopOf="parent" />-->
<LinearLayoutandroid:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="1">
    <SurfaceViewandroid:id="@+id/camera_surface_view1"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>
    <SurfaceViewandroid:id="@+id/camera_surface_view2"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>
</LinearLayout>
<LinearLayoutandroid:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="1">
    <SurfaceViewandroid:id="@+id/camera_surface_view3"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>
    <SurfaceViewandroid:id="@+id/camera_surface_view4"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>
</LinearLayout>
</LinearLayout>
相关推荐
千里马学框架3 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台3 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone3 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc3 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo3 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077003 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼3 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone3 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen3 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone3 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui