web-dev-qa-db-ja.com

Camerax - I / Zygote:以前に失敗したクラスjava.lang.Class <Androidx.core.View.ViewCompat $ 2>

CameraX Jetpackサポートライブラリを提供しています 入門ガイド 、a Github Repo 。エラーを得る

_I/zygote: Rejecting re-init on previously-failed class Java.lang.Class<androidx.core.view.ViewCompat$2>: 
Java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
_

サンプルアクティビティのレイアウトを設定すると(CameraActivity)。 APIレベル_26_上の仮想デバイスと実デバイスの両方でサンプルコードをテストしました。 CameraXライブラリのバージョンは_1.0.0-alpha03_これは最新の利用可能なバージョンです。

これが私のlayoutのコードです。

_<TextureView
    Android:id="@+id/textureView"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    Android:id="@+id/btnCapture"
    Android:layout_width="72dp"
    Android:layout_height="72dp"
    Android:scaleType="fitCenter"
    Android:layout_margin="24dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/ic_camera_accent"
    Android:background="?selectableItemBackgroundBorderless" />
_

これが私のactivityのコードです。

_public class CameraActivity extends AppCompatActivity {

    private TextureView textureView;
    private ImageButton btnCapture;

    private int REQUEST_CODE_CAMERA_WRITE_EXT = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera); // this line throws the error

        initComponents();
        checkPermissions();
    }

    private void initComponents() {
        textureView = findViewById(R.id.textureView);
        btnCapture = findViewById(R.id.btnCapture);
    }

    private void checkPermissions() {
        final String permissionCamera = Manifest.permission.CAMERA;
        final String permissionWriteExtStorage = Manifest.permission.WRITE_EXTERNAL_STORAGE;
        boolean isPermissionGranted = PermissionUtil.checkForPermission(this, permissionCamera);
        if (!isPermissionGranted) {
            boolean rationale = ActivityCompat.shouldShowRequestPermissionRationale(
                    CameraActivity.this, permissionCamera);
            if (rationale) {
                AlertDialog.Builder builder = new AlertDialog.Builder(CameraActivity.this);
                builder.setTitle("Camera Permission");
                builder.setMessage("In order to take the photo of your vein, you need to grant camera permission.");
                builder.setIcon(R.drawable.ic_info_accent);

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(CameraActivity.this, new String[]{permissionCamera, permissionWriteExtStorage}, REQUEST_CODE_CAMERA_WRITE_EXT);
                    }
                });

                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            } else
                ActivityCompat.requestPermissions(CameraActivity.this, new String[]{permissionCamera, permissionWriteExtStorage}, REQUEST_CODE_CAMERA_WRITE_EXT);
        } else
            startCamera();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CODE_CAMERA_WRITE_EXT) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                startCamera();
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void startCamera() {
        CameraX.unbindAll();

        int aspRatioW = textureView.getWidth(); //get width of screen
        int aspRatioH = textureView.getHeight(); //get height
        Rational asp = new Rational(aspRatioW, aspRatioH); //aspect ratio
        Size screen = new Size(aspRatioW, aspRatioH); //size of the screen

        PreviewConfig pConfig = new PreviewConfig.Builder().setTargetAspectRatio(asp).setTargetResolution(screen).build();
        Preview preview = new Preview(pConfig); //lets build it

        preview.setOnPreviewOutputUpdateListener(
                new Preview.OnPreviewOutputUpdateListener() {
                    //to update the surface texture we  have to destroy it first then re-add it
                    @Override
                    public void onUpdated(Preview.PreviewOutput output) {
                        ViewGroup parent = (ViewGroup) textureView.getParent();
                        parent.removeView(textureView);
                        parent.addView(textureView, 0);

                        textureView.setSurfaceTexture(output.getSurfaceTexture());
                        updateTransform();
                    }
                });


        ImageCaptureConfig imgCConfig = new ImageCaptureConfig.Builder().setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
                .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();
        final ImageCapture imgCap = new ImageCapture(imgCConfig);


        btnCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContextWrapper contextWrapper = new ContextWrapper(CameraActivity.this);
                String path = contextWrapper.getFilesDir().getPath();
                File file = new File(path + "/" + System.currentTimeMillis() + ".jpg");
                imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {
                    @Override
                    public void onImageSaved(@NonNull File file) {
                        String msg = "Photo capture succeeded: " + file.getAbsolutePath();
                        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(@NonNull ImageCapture.UseCaseError useCaseError, @NonNull String message, @Nullable Throwable cause) {
                        String msg = "Photo capture failed: " + message;
                        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
                        if (cause != null) {
                            cause.printStackTrace();
                        }
                    }
                });
            }
        });

        /* image analyser */

        ImageAnalysisConfig imgAConfig = new ImageAnalysisConfig.Builder().setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE).build();
        ImageAnalysis analysis = new ImageAnalysis(imgAConfig);

        analysis.setAnalyzer(
                new ImageAnalysis.Analyzer() {
                    @Override
                    public void analyze(ImageProxy image, int rotationDegrees) {
                        //y'all can add code to analyse stuff here idek go wild.
                    }
                });

        //bind to lifecycle:
        CameraX.bindToLifecycle((LifecycleOwner) this, analysis, imgCap, preview);
    }

    private void updateTransform() {
        //compensates the changes in orientation for the viewfinder, bc the rest of the layout stays in portrait mode.
        //methinks :thonk:
        Matrix mx = new Matrix();
        float w = textureView.getMeasuredWidth();
        float h = textureView.getMeasuredHeight();

        float cX = w / 2f; //calc centre of the viewfinder
        float cY = h / 2f;

        int rotationDgr;
        int rotation = (int) textureView.getRotation(); //cast to int bc switches don't like floats

        switch (rotation) { //correct output to account for display 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);
        textureView.setTransform(mx); //apply transformations to textureview
    }
}
_
22
talha06

私の問題を解決しました。

build.gradle(アプリレベル)

//依存関係を追加します

implementation 'androidx.core:core:1.5.0-alpha04'
 _
1
Shomu