简体   繁体   中英

Android Studio CameraX Module

I am working on an app that can open camera to take picture and then show it on Imageview in the next activity or select picture from file and then view it in ImageView on next activity.

I have a java class named CameraActivity for my App. Below is a code I want to use. It was working before on another app. But with Android Studio Update it is not working in new App.

I guess It's depreciated.I need this code to wokk. Help me with necessary changes.

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);
    }
}

I am attaching he images below to help with identifying the errors在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Use these dependencies.

// 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM