【问题标题】:Android Studio CameraX ModuleAndroid Studio CameraX 模块
【发布时间】:2023-01-07 19:29:33
【问题描述】:

我正在开发一个应用程序,它可以打开相机拍照,然后在下一个活动中将其显示在 Imageview 上,或者从文件中选择图片,然后在下一个活动中在 ImageView 中查看它。

我的应用程序有一个名为 CameraActivity 的 java 类。 下面是我想使用的代码。它之前在另一个应用程序上工作过。但是对于 Android Studio 更新,它在新应用程序中不起作用。

估计贬值了我需要这段代码才能工作。帮助我进行必要的更改。

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.core.CameraX;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureConfig;
import androidx.camera.core.Preview;
import androidx.camera.core.PreviewConfig;
import androidx.lifecycle.LifecycleOwner;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Rational;
import android.util.Size;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;

public class CameraActivity extends AppCompatActivity {
    TextureView viewFinder;
    ImageView imgCapture;
    private static final int CAMERA_REQUEST = 1888;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        viewFinder=findViewById(R.id.viewFinder);
        imgCapture = findViewById(R.id.imgCapture);
        startCamera();
    }

    @SuppressLint("RestrictedApi")
     public void startCamera(){

        CameraX.unbindAll();
        Rational aspectRatio = new Rational(viewFinder.getWidth(),viewFinder.getHeight());
        Size screen = new Size(viewFinder.getWidth(),viewFinder.getHeight());
        PreviewConfig previewConfig = new PreviewConfig.Builder().setTargetAspectRatio(aspectRatio).setTargetResolution(screen).build();
        Preview preview = new Preview(previewConfig);

        preview.setOnPreviewOutputUpdateListener(new Preview.OnPreviewOutputUpdateListener() {
            @Override
            public void onUpdated(Preview.PreviewOutput output) {
                ViewGroup parent = (ViewGroup)viewFinder.getParent();
                parent.removeView(viewFinder);
                parent.addView(viewFinder,0);
                viewFinder.setSurfaceTexture(output.getSurfaceTexture());
                updateTransform();
            }
        });
        ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder().setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY).setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();
        final ImageCapture imageCapture = new ImageCapture(imageCaptureConfig);
        imgCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(Environment.getExternalStorageDirectory()+"/"+System.currentTimeMillis()+".jpg");
                imageCapture.takePicture(file, new ImageCapture.OnImageSavedListener() {
                    @SuppressLint("ShowToast")
                    @Override
                    public void onImageSaved(@NonNull File file) {
                        Toast.makeText(getApplicationContext(),"Pic Captured at" + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(CameraActivity.this,ShowPhotoActivity.class);
                        intent.putExtra("path",file.getAbsoluteFile()+"");
                        startActivity(intent);
                    }
                    @Override
                    public void onError(@NonNull ImageCapture.UseCaseError useCaseError, @NonNull String message, @Nullable Throwable cause) {
                        Toast.makeText(getApplicationContext(),"Pic Captured Failed! " + message, Toast.LENGTH_SHORT).show();
                        if(cause!=null){
                            cause.printStackTrace();
                        }
                    }
                });
            }
        });
        CameraX.bindToLifecycle((LifecycleOwner)this, preview, imageCapture);
    }
    public void updateTransform(){

        Matrix mx = new Matrix();
        float w = viewFinder.getMeasuredWidth();
        float h = viewFinder.getMeasuredHeight();
        float cx = w/2f;
        float cy = h/2f;
        int rotationDgr = 90;
        int rotation = (int)viewFinder.getRotation();
        switch(rotation){
            case Surface.ROTATION_0:
                rotationDgr = 0;
                break;
            case Surface.ROTATION_90:
                rotationDgr = 90;
                break;
            case Surface.ROTATION_180:
                rotationDgr = 180;
                break;
            case Surface.ROTATION_270:
                rotationDgr = 270;
                break;
            default:
                return;
        }
        mx.postRotate((float)rotationDgr,cx,cy);
        viewFinder.setTransform(mx);
    }
}

我在下面附上他的图片以帮助识别错误

【问题讨论】:

  • 请将代码和错误消息添加为文本而不是图像
  • 我还将代码添加为文本。图片仅供参考,大家可以看看是哪一行出错了

标签: java android android-studio android-camerax


【解决方案1】:

使用这些依赖项。

// CameraX
implementation "androidx.camera:camera-core:1.2.0-beta02"
// link UI's lifecycle with camera-x (It will handle by itself)
implementation "androidx.camera:camera-lifecycle:1.2.0-beta02"
// Get camera-x's previewView
implementation "androidx.camera:camera-view:1.2.0-beta02"
// For bokeh & else
implementation "androidx.camera:camera-extensions:1.2.0-beta02"
// Added for choosing best quality Camera Selection
implementation 'androidx.camera:camera-camera2:1.1.0'

【讨论】:

  • 检查 AAR 元数据时发现 5 个问题: 1. 依赖项“androidx.camera:camera-view:1.2.0-rc01”需要依赖它的库和应用程序才能针对版本 33 或更高版本的 Android API 进行编译。
  • :app 目前是针对 android-32 编译的。建议的操作:更新此项目以使用至少为 33 的更新的 compileSdkVersion,例如 33。
  • 请注意,更新库或应用程序的 compileSdkVersion(它允许使用更新的 API)可以与更新 targetSdkVersion(它选择应用程序进入新的运行时行为)和 minSdkVersion(它确定应用程序可以安装在哪些设备上)分开完成。 2. 依赖项“androidx.camera:camera-lifecycle:1.2.0-rc01”需要依赖它的库和应用程序针对版本 33 或更高版本的 Android API 进行编译。
  • 试试这个,我已经更新了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 2017-03-25
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-10
相关资源
最近更新 更多