当前正在试用新的CameraX。任何人都知道有什么问题。我应该拥有所需的所有权限。
这是我的全部日志猫。
2019-12-08 03:57:56.716 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.717 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.717 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.717 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.718 11667-11696/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.718 11667-11696/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.759 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:57.192 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:57.215 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:57.239 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:57.242 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:57.542 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:57.552 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:57.556 11667-11667/com.example.camerax E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
2019-12-08 03:57:57.557 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:57.585 11667-11708/com.example.camerax E/libc: Access denied finding property "persist.vendor.camera.privapp.list"
2019-12-08 03:57:57.891 11667-11708/com.example.camerax E/Camera: Unable to open camera 0 due to CAMERA_DISABLED (1): validateClientPermissionsLocked:928: Caller "com.example.camerax" (PID 10280, UID 11667) cannot open camera "0" from background
2019-12-08 03:57:57.899 11667-11708/com.example.camerax E/Camera: CameraDevice.onError(): 0 with error: ERROR_CAMERA_DISABLED
2019-12-08 03:57:57.901 11667-11708/com.example.camerax E/AndroidRuntime: FATAL EXCEPTION: CameraX-
Process: com.example.camerax, PID: 11667
java.lang.IllegalStateException: onError() should not be possible from state: INITIALIZED
at androidx.camera.camera2.impl.Camera$StateCallback.onError(Camera.java:850)
at androidx.camera.core.CameraDeviceStateCallbacks$ComboDeviceStateCallback.onError(CameraDeviceStateCallbacks.java:120)
at android.hardware.camera2.impl.CameraDeviceImpl$8.run(CameraDeviceImpl.java:355)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:65)
它显然尝试了很多,然后放弃了。 鉴于在此以及在Google上都完全没有相关的文章,我将为您提供所有代码。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.camerax">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.camerax"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0-alpha05'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
def camerax_version = "1.0.0-alpha01"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
package com.example.login3;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraX;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageAnalysisConfig;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureConfig;
import androidx.camera.core.ImageProxy;
import androidx.camera.core.Preview;
import androidx.camera.core.PreviewConfig;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;
import android.content.pm.PackageManager;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Environment;
import android.util.Rational;
import android.util.Size;
import android.view.LayoutInflater;
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;
/**
* A simple {@link Fragment} subclass.
*/
public class FeedFragment extends Fragment {
public FeedFragment() {
// Required empty public constructor
}
private int REQUEST_CODE_PERMISSIONS = 10; //arbitrary number, can be changed accordingly
private final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.CAMERA","android.permission.WRITE_EXTERNAL_STORAGE"}; //array w/ permissions from manifest
private TextureView txView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_feed, container, false);
txView = v.findViewById(R.id.view_finder);
if(allPermissionsGranted()){
startCamera(); //start camera if permission has been granted by user
} else{
ActivityCompat.requestPermissions(getActivity(), REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS);
}
return v;
}
private void startCamera() {
//make sure there isn't another camera instance running before starting
CameraX.unbindAll();
/* start preview */
int aspRatioW = txView.getWidth(); //get width of screen
int aspRatioH = txView.getHeight(); //get height
Rational asp = new Rational (aspRatioW, aspRatioH); //aspect ratio
Size screen = new Size(aspRatioW, aspRatioH); //size of the screen
//config obj for preview/viewfinder thingy.
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) txView.getParent();
parent.removeView(txView);
parent.addView(txView, 0);
txView.setSurfaceTexture(output.getSurfaceTexture());
updateTransform();
}
});
/* image capture */
//config obj, selected capture mode
ImageCaptureConfig imgCapConfig = new ImageCaptureConfig.Builder().setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
.setTargetRotation(getActivity().getWindowManager().getDefaultDisplay().getRotation()).build();
final ImageCapture imgCap = new ImageCapture(imgCapConfig);
getActivity().findViewById(R.id.capture_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = new File(Environment.getExternalStorageDirectory() + "/" + 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(getActivity(), 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(getActivity(), msg,Toast.LENGTH_LONG).show();
if(cause != null){
cause.printStackTrace();
}
}
});
}
});
//bind to lifecycle:
CameraX.bindToLifecycle((LifecycleOwner)this, 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:
* imgCap does this already, this class can be commented out or be used to optimise the preview
*/
Matrix mx = new Matrix();
float w = txView.getMeasuredWidth();
float h = txView.getMeasuredHeight();
float centreX = w / 2f; //calc centre of the viewfinder
float centreY = h / 2f;
int rotationDgr;
int rotation = (int)txView.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, centreX, centreY);
txView.setTransform(mx); //apply transformations to textureview
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//start camera when permissions have been granted otherwise exit app
if(requestCode == REQUEST_CODE_PERMISSIONS){
if(allPermissionsGranted()){
startCamera();
} else{
Toast.makeText(getActivity(), "Permissions not granted by the user.", Toast.LENGTH_SHORT).show();
getActivity().finish();
}
}
}
private boolean allPermissionsGranted(){
//check if req permissions have been granted
for(String permission : REQUIRED_PERMISSIONS){
if(ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}
}
我什至尝试从https://stackoverflow.com/a/56806751/12253594实现代码,但这没用。
最诚挚的问候。