gpt4 book ai didi

java - 尝试使用已破坏的生命周期创建 LifecycleCamera

转载 作者:行者123 更新时间:2023-12-04 12:02:32 24 4
gpt4 key购买 nike

我正在尝试为我的屏幕录像机应用程序制作 float cameraview。
FloatingViewService.java

public class FloatingViewService extends LifecycleService implements CameraXConfig.Provider, LifecycleOwner {
private View mFloatingView;
private WindowManager mWindowManager;
PreviewView previewView;
private ListenableFuture<ProcessCameraProvider> cameraProviderFuture;

public FloatingViewService() {
}


@Nullable
@Override
public IBinder onBind(Intent intent) {
super.onBind(intent);
return null;
}

@Override
public void onCreate() {
super.onCreate();
mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
final WindowManager.LayoutParams params;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);

} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);

}
params.width = 400;
params.height = 300;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);


mFloatingView.findViewById(R.id.previewFrame).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;

case MotionEvent.ACTION_UP:
return true;

case MotionEvent.ACTION_MOVE:
//this code is helping the widget to move around the screen with fingers
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
mWindowManager.updateViewLayout(mFloatingView, params);
return true;
}
return false;
}
});

cameraFun(mFloatingView);


}

public void cameraFun(View mFloatingView) {
previewView = mFloatingView.findViewById(R.id.previewFrame);
cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
bindPreview(cameraProvider);
} catch (ExecutionException | InterruptedException e) {
}
}, ContextCompat.getMainExecutor(this));
}

void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
Preview preview = new Preview.Builder()
.build();

CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_FRONT)
.build();
preview.setSurfaceProvider(previewView.createSurfaceProvider());
OrientationEventListener orientationEventListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int orientation) {
int rotation;
int orientationcheck = getResources().getConfiguration().orientation;

if (orientationcheck == Configuration.ORIENTATION_PORTRAIT) {
if (orientation >= 45 && orientation < 135) {
rotation = 360;
} else if (orientation >= 135 && orientation < 225) {
rotation = 180;
} else if (orientation >= 225 && orientation < 315) {
rotation = 360;
} else {
rotation = 0;
}
Log.i("orientation", String.valueOf(orientation) + "= ORIENTATION_PORTRAIT" + orientationcheck);

} else {
if (orientation >= 45 && orientation < 135) {
rotation = 90;
} else if (orientation >= 135 && orientation < 225) {
rotation = 180;
} else if (orientation >= 225 && orientation < 315) {
rotation = 270;
} else {
rotation = 0;
}
Log.i("orientation", String.valueOf(orientation) + "= ORIENTATION_Land" + orientationcheck);
}
Log.i("orientation", String.valueOf(orientation) + "= " + orientationcheck);

previewView.setRotation(rotation);
}
};
orientationEventListener.enable();
cameraProvider.bindToLifecycle(this, cameraSelector, preview);
}

@NonNull
@Override
public CameraXConfig getCameraXConfig() {
return Camera2Config.defaultConfig();
}

public void onDestroy() {
super.onDestroy();
mWindowManager.removeView(mFloatingView);
}
}
layout_floating_widget.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.camera.view.PreviewView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/previewFrame"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center" />
后台服务.java 检查下面的代码我如何调用 FloatingViewService.java
public class BackgroundService extends Service {
Boolean onoff = true;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

public void onCreate() {
super.onCreate();
new FloatingViewService();
if (onoff) {
onoff = false;
startService(new Intent(this, FloatingViewService.class));
} else {
onoff = true;
stopService(new Intent(this, FloatingViewService.class));
}
}
}
错误:
 java.lang.IllegalArgumentException: Trying to create LifecycleCamera with destroyed lifecycle.
at androidx.camera.lifecycle.LifecycleCameraRepository.createLifecycleCamera(LifecycleCameraRepository.java:103)
at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:414)
at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:275)
at com.rdbrain.FloatingViewService.bindPreview(FloatingViewService.java:196)
at com.rdbrain.FloatingViewService.lambda$cameraFun$0$FloatingViewService(FloatingViewService.java:138)
at com.rdbrain.-$$Lambda$FloatingViewService$qkM-fNI79D-TvnilmrNFZjQYwlI.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
备注 :此代码作为单独的模块工作,但是当我连接到屏幕录像机时它不起作用,我已经在 manifests.xml 中设置了所有必要的权限和服务

最佳答案

使用查看生命周期所有者 而不是 这个cameraProvider.bindToLifecycle(this, cameraSelector, preview);

关于java - 尝试使用已破坏的生命周期创建 LifecycleCamera,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63276332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com