Camera.open() 'open' keeps losing money

Asked 2 years ago, Updated 2 years ago, 28 views

We are developing a camera application.

I'd like to display the image obtained from the camera on SurfaceView, but I can't.
"When I tried to copy the sutra, the title ""open"" remained in the red."
"Other than that, ""release"" and ""setPreviewDisplay"" are also in the red."
I'd like to use Android.hardware.Camera (or Camera2) for import, but I can't. Horizontal lines are drawn when used.

I have no idea what's wrong.Could you tell me?

Here's the code:

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraDevice;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.LinearLayout;

public class MainActivity extensions AppCompatActivity{

    private boolean checkCameraHardware (Context context) {
        if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            // This device has a camera
            return true;
        } else{
            // no camera on this device
            return false;
        }
    }

    public static CameraDevice getCametaInstance(){
        CameraDevice c=null;
        try{
            c=CameraDevice.open();//attempt to get a Camera instance
        }
        catch(Exceptione){
            // Camera is not available (in use or does not exist)
        }
        return c;// returns null if camera is unavailable
    }

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

}

java android

2022-09-30 14:35

1 Answers

There is no open() method in the new android.hardware.camera2.CameraDevice class.

The old android.hardware.Camera class has the open() method, which itself is deprecated at API level 21 (Android 5.0) or later.
See Note at the beginning of the reference below.

If you are targeting Android 5.0 or later, you must use the new Camera 2 API.

It is not recommended that you try to support Android 4.x or earlier now, but if you really want to target Android 4.x or earlier, you can also use the compatible library androidx.camera to absorb differences in Android OS versions.However, androidx.camera is still in beta as of July 2020.

Android is an OS that frequently changes specifications and eliminates and replaces API sets, so you shouldn't refer to cheap books that quickly become obsolete.
(Only a few books, like the Android kernel, will not become obsolete.)


2022-09-30 14:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.