Camera1 调用摄像机预览+获取每一帧

本文详细介绍了如何使用Camera1 API在Android系统中进行相机预览并获取每一帧数据,通过MainActivity和特定布局文件实现。在onPreviewFrame方法中处理返回的数据,但需要注意数据格式转换。由于在树莓派3b上的androidthings系统中,Camera1 API不可用,建议转用Camera2 API,因为V4L2接口与Android HAL不完全兼容,不支持同时配置多个流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用camera1调试相机预览+获取每一帧数据,

https://www.jianshu.com/p/3440d82545f6

https://www.jianshu.com/p/705d4792e836

主要学习代码,在这里

MainActivity

public class MainActivity extends AppCompatActivity implements Handler.Callback {

    private static final int MSG_OPEN_CAMERA = 1;
    private static final int MSG_CLOSE_CAMERA = 2;
    private static final int MSG_SET_PREVIEW_SIZE = 3;
    private static final int MSG_SET_PREVIEW_SURFACE = 4;
    private static final int MSG_START_PREVIEW = 5;
    private static final int MSG_STOP_PREVIEW = 6;
    private static final int MSG_SET_PICTURE_SIZE = 7;
    private static final int MSG_TAKE_PICTURE = 8;

    private static final String TAG = "MainActivity";
    private static final int REQUEST_PERMISSIONS_CODE = 1;
    private static final String[] REQUIRED_PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private static final int PREVIEW_FORMAT = ImageFormat.NV21;

    @Nullable
    private HandlerThread mCameraThread = null;

    @Nullable
    private Handler mCameraHandler = null;

    @Nullable
    private Camera.CameraInfo mFrontCameraInfo = null;
    private int mFrontCameraId = -1;

    @Nullable
    private Camera.CameraInfo mBackCameraInfo = null;
    private int mBackCameraId = -1;

    @Nullable
    private Camera mCamera;
    private int mCameraId = -1;
    private Camera.CameraInfo mCameraInfo;

    @Nullable
    private SurfaceHolder mPreviewSurface;
    private int mPreviewSurfaceWidth;
    private int mPreviewSurfaceHeight;

    @Nullable
    private DeviceOrientationListener mDeviceOrientationListener;

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_OPEN_CAMERA: {
                openCamera(msg.arg1);
                break;
            }
            case MSG_CLOSE_CAMERA: {
                closeCamera();
                break;
            }
            case MSG_SET_PREVIEW_SIZE: {
                int shortSide = msg.arg1;
                int longSide = msg.arg2;
                setPreviewSize(shortSide, longSide);
                break;
            }
            case MSG_SET_PREVIEW_SURFACE: {
                SurfaceHolder previewSurface = (SurfaceHolder) msg.obj;
                setPreviewSurface(previewSurface);
                break;
            }
            case MSG_START_PREVIEW: {
                startPreview();
                break;
            }
            case MSG_STOP_PREVIEW: {
                stopPreview();
                break;
            }
            case MSG_SET_PICTURE_SIZE: {
                int shortSide = msg.arg1;
                int longSide = msg.arg2;
                setPictureSize(shortSide, longSide);
                break;
            }
            case MSG_TAKE_PICTURE: {
                takePicture();
            }
            default:
                throw new IllegalArgumentException("Illegal message: " + msg.what);
        }
        return false;
    }

    private ImageView iv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDeviceOrientationListener = new DeviceOrientationListener(this);

        startCameraThread();

        initCameraInfo();
        iv1=findViewById(R.id.imageView);
        SurfaceView cameraPreview = findViewById(R.id.camera_preview);
        cameraPreview.getHolder().addCallback(new PreviewSurfaceCallback());

        Button switchCameraButton = findViewById(R.id.switch_camera);
        switchCameraButton.setOnClickListener(new OnSwitchCameraButtonClickListener());

        Button takePictureButton = findViewById(R.id.take_picture);
        takePictureButton.setOnClickListener(new OnTakePictureButtonClickListener());
    }

    @Override
    protected void onStart() {
        super.onStart();
        DeviceOrientationListener deviceOrientationListener = mDeviceOrientationListener;
        if (deviceOrientationListener != null) {
            deviceOrientationListener.enable();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 动态权限检查
        if (!isRequiredPermissionsGranted() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(REQUIRED_PERMISSIONS, REQUEST_PERMISSIONS_CODE);
        } else if (mCameraHandler != null) {
            mCameraHandler.obtainMessage(MSG_OPEN_CAMERA, getCameraId(), 0).sendToTarget();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mCameraHandler != null) {
            mCameraHandler.removeMessages(MSG_OPEN_CAMERA);
            mCameraHandler.sendEmptyMessage(MSG_CLOSE_CAMERA);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        DeviceOrientationListener deviceOrientationListe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值