标签: android-camera2

如何在Android中使用Camera2 API录制视频时以编程方式拍摄照片

我想在使用camera2 API录制视频时捕获图像.

有两个独立的演示.1. 捕获图像和2. 录制视频

我试图将它们组合成一个应用程序.但在很多事情上都很困惑.喜欢

  • CameraDevice是单个,表示连接到Android设备的单个摄像头.
  • 请求中需要不同的模板.对于图像:mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);和视频mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
  • 在createCaptureSession方法中,需要ImageReader的表面,并且需要视频MediaRecorder.

在图像和视频的createCaptureSession方法中需要CameraCaptureSession.StateCallback.

而camera2 API是新的.因此,Google上没有更多示例.

有人对此有任何想法吗?喜欢如何在Android中录制时捕捉照片?

video android image android-camera2

8
推荐指数
2
解决办法
5289
查看次数

Android camera2输出到ImageReader格式YUV_420_888仍然很慢

我正在尝试让Android camera2在后台服务中运行,然后在回调ImageReader.OnImageAvailableListener中处理帧.我已经使用建议的原始格式YUV_420_888来获得最大fps,但是我只能在分辨率640x480上获得7fps左右.这甚至比我使用旧的Camera接口(我想升级到Camera2以获得更高的fps)或使用OpenCV JavaCameraView(我不能使用它,因为我需要在后台服务中运行处理)更慢.

以下是我的服务类.我错过了什么?

我的手机是运行Android 5.0.2的Redmi Note 3

public class Camera2ServiceYUV extends Service {
    protected static final String TAG = "VideoProcessing";
    protected static final int CAMERACHOICE = CameraCharacteristics.LENS_FACING_BACK;
    protected CameraDevice cameraDevice;
    protected CameraCaptureSession captureSession;
    protected ImageReader imageReader;

    // A semaphore to prevent the app from exiting before closing the camera.
    private Semaphore mCameraOpenCloseLock = new Semaphore(1);


    public static final String RESULT_RECEIVER = "resultReceiver";
    private static final int JPEG_COMPRESSION = 90;

    public static final int RESULT_OK = 0;
    public static final …
Run Code Online (Sandbox Code Playgroud)

android opencv android-camera android-camera2

8
推荐指数
1
解决办法
2126
查看次数

Android Camera2焦点状态卡住了

我需要在我的应用程序上使用Camera2 API.(Api21 +)我找到了下一个样本:https: //github.com/googlesamples/android-Camera2Basic

我下载了它并开始使用我的手机.当我按下"图片"按钮时,它会调用takePhoto方法.

private void takePicture() {
    lockFocus();
}
Run Code Online (Sandbox Code Playgroud)

这是一台国家机器.有时这台机器卡住了STATE_WAITING_LOCK.我的设备正在等待Focus,但没有任何反应!(是的,我的设备支持自动对焦)

case STATE_WAITING_LOCK: {
        Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
        if (afState == null) {
            captureStillPicture();
        } else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
                CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
            // CONTROL_AE_STATE can be null on some devices
            Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
            if (aeState == null ||
                    aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
                mState = STATE_PICTURE_TAKEN;
                captureStillPicture();
            } else {
                runPrecaptureSequence();
            }
        }
        break;
    }
Run Code Online (Sandbox Code Playgroud)

这个问题有什么好的解决方案?这个程序有时会在这里崩溃:

private void unlockFocus() {
    try { …
Run Code Online (Sandbox Code Playgroud)

android android-camera2

8
推荐指数
1
解决办法
1566
查看次数

在 Android 上 - 如何从广角相机捕获图像/视频?

如何从camera2 api广角相机捕捉图像或视频?还是望远镜?我知道如何处理前置和后置摄像头的摄像头捕获。我就是不明白如何打开相机并选择广角/伸缩相机?

我想这与设置以下其中一项有关:

CameraMetadata.REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA
CameraCharacteristics.getPhysicalCameraIds()
CameraCharacteristics.getAvailablePhysicalCameraRequestKeys()
CameraDevice.createCaptureSession(SessionConfiguration config)
CameraCharactersitics.LOGICAL_MULTI_CAMERA_SENSOR_SYNC_TYPE
Run Code Online (Sandbox Code Playgroud)

但是我无法理解设置它的场景,我没有找到任何好的解释。我将感谢任何类型的教程或解释。最后一个问题 - 如何在没有物理设备的情况下对其进行测试?我的意思是 - 如何设置 Avd/模拟器?

camera android android-camera2

8
推荐指数
1
解决办法
1328
查看次数

Android 10 (api 29) camera2 api 回归与广角相机

在此处输入图片说明我在专为 Google Pixel 3 XL 设计的相机应用中使用了 camera2 api。该设备有两个前置摄像头(广角和普通)。多亏了多摄像头功能,我可以同时访问两个物理摄像头设备,我的应用程序具有在这两个摄像头之间切换的功能。在我最近升级到 Android 10 之前,我可以准确地看到两个不同的结果,但现在我的广角捕捉帧与普通相机具有几乎相同的 FOV(视野)。因此,Android 9 广角捕获结果上的相同代码、相同 apk 是宽的,正如预期的那样,并且在 Andoird 10 升级之后 - 广角和普通相机显示几乎相同的 FOV。

这是一个代码片段,用于演示我如何初始化两个摄像头并捕获预览:

主活动.kt

 private val surfaceReadyCallback = object: SurfaceHolder.Callback {
        override fun surfaceChanged(p0: SurfaceHolder?, p1: Int, p2: Int, p3: Int) { }
        override fun surfaceDestroyed(p0: SurfaceHolder?) { }

        override fun surfaceCreated(p0: SurfaceHolder?) {

            // Get the two output targets from the activity / fragment
            val surface1 = surfaceView1.holder.surface  
            val surface2 = surfaceView2.holder.surface 

            val dualCamera = findShortLongCameraPair(cameraManager)!!
            val outputTargets = …
Run Code Online (Sandbox Code Playgroud)

android android-camera android-camera2 android-10.0

8
推荐指数
1
解决办法
3193
查看次数

Android CameraX 如何实现广角/缩小

我尝试使用CameraX api为我的应用内相机实现广角选项,但遇到了一个问题 -CameraControl.setZoomRatio允许在ZoomState.getMinZoomRatio()和之间设置变焦ZoomState.getMaxZoomRatio(),在我测试的手机上minZoomRatio1.0f。同一款手机支持缩小到0.5f系统相机内。

当前片段我如何初始化相机:

private var camera: Camera? = null
private var imageCapture: ImageCapture? = null

private fun startCamera() {
    val cameraProviderFuture = ProcessCameraProvider.getInstance(this)

    cameraProviderFuture.addListener({
            val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()

            val preview = Preview.Builder()
                .build()
                .also {
                    it.setSurfaceProvider(binding.viewFinder.surfaceProvider)
                }

            imageCapture = ImageCapture.Builder()
                .setFlashMode(ImageCapture.FLASH_MODE_AUTO)
                .build()

            val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

            try {
                cameraProvider.unbindAll()

                camera = cameraProvider.bindToLifecycle(
                    this, cameraSelector, preview, imageCapture
                )
                viewModel.onCameraStarted()
            } catch (exc: Exception) {
                Timber.e(exc)
            }
        }, …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-camera2 android-camerax

8
推荐指数
1
解决办法
2805
查看次数

接收 CameraAccessException: CAMERA_ERROR (3) on CaptureSession.setRepeatingRequest()

我从 onCreate() 方法运行以下代码。这段代码之外只是回调的许多对象声明和实例化。

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

    thisAct = this;
    display = (TextureView)findViewById(R.id.display);
    display.setSurfaceTextureListener(surfaceTextureListener);

    db = openOrCreateDatabase("MyDatabase", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS Photos (ID INTEGER, location TEXT, size INTEGER)");
    db.execSQL("CREATE TABLE IF NOT EXISTS Tags (ID INTEGER, tag TEXT)");

    camMan = this.getSystemService(CameraManager.class);

    new Thread(new Runnable(){
        @Override
        public void run() {
            try {
                String cm = camMan.getCameraIdList()[0];

                if ( ContextCompat.checkSelfPermission(thisAct, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ) {
                    ActivityCompat.requestPermissions((Activity)thisAct, new String[] { Manifest.permission.CAMERA }, CAMERA_REQUEST);
                }
                camMan.openCamera(cm, CDstateCallback, …
Run Code Online (Sandbox Code Playgroud)

java hardware android android-camera2

7
推荐指数
1
解决办法
5177
查看次数

致命异常:java.lang.IllegalArgumentException:未注册接收者:android.hardware.camera2.CameraManager

我正在开发一个手电筒应用程序,可以打开/关闭系统功能。

我遇到了在Android M(v6.0)上显示的崩溃

速溶药

这是问题详细信息和堆栈跟踪:

Fatal Exception: java.lang.IllegalArgumentException: Receiver not registered: android.hardware.camera2.CameraManager$1@49e5f1b
   at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:789)
   at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1222)
   at android.hardware.camera2.CameraManager$3.run(CameraManager.java:1266)
   at android.os.Handler.handleCallback(Handler.java:815)
   at android.os.Handler.dispatchMessage(Handler.java:104)
   at android.os.Looper.loop(Looper.java:207)
   at android.app.ActivityThread.main(ActivityThread.java:5728)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Run Code Online (Sandbox Code Playgroud)

我具有以下Manifest权限和硬件功能:

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.flash" />
Run Code Online (Sandbox Code Playgroud)

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
Run Code Online (Sandbox Code Playgroud)

****编辑:****

这是我用来访问摄像机的代码:

// Here, I am checking if SDK >= M
if (VersionUtils.isMarshmallowOrGreater()) {

        cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

        if (cameraManager != null) {

            try {
                cameraId = cameraManager.getCameraIdList()[0];
            } catch (CameraAccessException | …
Run Code Online (Sandbox Code Playgroud)

android illegalargumentexception receiver android-broadcastreceiver android-camera2

7
推荐指数
1
解决办法
571
查看次数

如何在Camera2 API Android中捕获覆盖层内的图像?

叠加层内的图像

我只需要捕获如图所示的覆盖框内的图像。我不需要TextureView 中的整个图像。我是 Android 新手,我无法找出一些提到的仅捕获矩形区域内图像的解决方案。覆盖层必须位于中心。我尝试过,但没能实现。

我遵循了这段代码 - Google Sample-Camera2Basic


为了添加覆盖,我使用了以下代码:

activity_camera2_basic_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.googlecamera2.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_centerInParent="true"/>

    <FrameLayout
        android:id="@+id/control"
        android:layout_width="match_parent"
        android:layout_height="112dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@color/control_background">

        <Button
            android:id="@+id/picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/picture" />

        <ImageButton
            android:id="@+id/info"
            android:contentDescription="@string/description_info"
            style="@android:style/Widget.Material.Light.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:padding="20dp"
            android:src="@drawable/ic_action_info" />

    </FrameLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我是onCreateView这样实现的:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_camera2_basic, container, false);

        linearLayout = view.findViewById(R.id.surface);
        linearLayout.addView(new Rectangle(getActivity())); …
Run Code Online (Sandbox Code Playgroud)

android overlay crop android-camera2

7
推荐指数
1
解决办法
6986
查看次数

camera2如何在图像阅读器侦听器中从YUV_420_888图像获取Exif数据

我正在尝试从YUV_420_888图像获取Exif数据,但是它不起作用。我尝试了几种解决方案,例如将图像以jpeg格式保存到磁盘,将其转换为输入流,但似乎无济于事。

我使用Android camera2 API捕获YUV_420_888图像。然后在OnImageAvailableListener中获取图像,并尝试使用ExifInterface API读取其EXIF数据。但是它总是空的。我尝试了此链接中的所有方法以获得正确的字节数组。

这是我的代码:

@Override
public void onImageAvailable(ImageReader imageReader) {
    if (!isRecording) {
        return;
    }
    Image image = imageReader.acquireNextImage();
    File file = Util.getImagePath(context);

    OutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(file);
        outputStream.write(data); 
//// This byte array I am making using all the approaches given in this link 
/sf/ask/3081544371/

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    try {
        ExifInterface exifInterface = new …
Run Code Online (Sandbox Code Playgroud)

android exif yuv android-camera2

7
推荐指数
1
解决办法
245
查看次数