Skip to content

Commit

Permalink
fix(android): black screen issue after remount deepar component (reso…
Browse files Browse the repository at this point in the history
…lves #8)
  • Loading branch information
ridvanaltun committed Jul 30, 2022
1 parent f766b17 commit 5a7f830
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 76 deletions.
49 changes: 34 additions & 15 deletions android/src/main/java/com/reactnativedeepar/CameraService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.reactnativedeepar;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Size;
import android.view.Surface;
Expand Down Expand Up @@ -72,19 +75,37 @@ public void analyze(@NonNull ImageProxy image) {
private int width = 0;
private int height = 0;
private DeepAR frameReceiver;
private CameraManager cameraManager;

public CameraService(Activity mContext) {
context = mContext;
cameraManager = (CameraManager) this.context.getSystemService(Context.CAMERA_SERVICE);
setupCamera();
}

private void setupCamera() {
cameraProviderFuture = ProcessCameraProvider.getInstance(this.context);
cameraProviderFuture.addListener(new Runnable() {
@Override
public void run() {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
bindImageAnalysis(cameraProvider);
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
}, ContextCompat.getMainExecutor(this.context));
}

public void openCamera(DeepAR frameReceiver) {
this.frameReceiver = frameReceiver;
setupCamera();
}

public void closeCamera() {
ProcessCameraProvider cameraProvider = null;

try {
cameraProvider = cameraProviderFuture.get();
cameraProvider.unbindAll();
Expand All @@ -93,14 +114,27 @@ public void closeCamera() {
} catch (InterruptedException e) {
e.printStackTrace();
}

if (surfaceProvider != null) {
surfaceProvider.stop();
surfaceProvider = null;
}

frameReceiver.release();
frameReceiver = null;
}

public void setFlashOn(boolean enabled) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String mCameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(mCameraId, enabled);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public void switchCamera(int _lensFacing) {
lensFacing = _lensFacing == CameraSelector.LENS_FACING_FRONT ? CameraSelector.LENS_FACING_FRONT : CameraSelector.LENS_FACING_BACK;
//unbind immediately to avoid mirrored frame.
Expand All @@ -116,21 +150,6 @@ public void switchCamera(int _lensFacing) {
setupCamera();
}

private void setupCamera() {
cameraProviderFuture = ProcessCameraProvider.getInstance(this.context);
cameraProviderFuture.addListener(new Runnable() {
@Override
public void run() {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
bindImageAnalysis(cameraProvider);
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
}, ContextCompat.getMainExecutor(this.context));
}

private void bindImageAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
CameraResolutionPreset cameraResolutionPreset = CameraResolutionPreset.P1920x1080;
int width;
Expand Down
86 changes: 39 additions & 47 deletions android/src/main/java/com/reactnativedeepar/RNTDeepAR.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.camera2.CameraManager;
import android.media.Image;
import android.os.Build;
import android.os.Environment;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleObserver;

import com.facebook.react.bridge.ReactContext;
Expand All @@ -51,34 +46,25 @@ public class RNTDeepAR extends FrameLayout implements AREventListener, SurfaceHo
String tempVideoPath;
private DeepAR deepAr;
private CameraService cameraService;
private CameraManager mCameraManager;
private boolean started;

public RNTDeepAR(Context context, DeepAR _deepAr) {
public RNTDeepAR(Context context) {
super(context);
deepAr = _deepAr;
init();
}

public RNTDeepAR(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
deepAr = new DeepAR(context);
setupDeepAR();
}

public RNTDeepAR(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
@Override
protected void onDetachedFromWindow() {
cameraService.closeCamera();
super.onDetachedFromWindow();
}

public void init() {
private void setupDeepAR() {
View view = inflate(getContext(), R.layout.deeparview, null);
addView(view);

deepAr.initialize(this.getContext(), this);
setupDeepAR();
}

private void setupDeepAR() {

if (started) {
return;
Expand All @@ -92,18 +78,13 @@ private void setupDeepAR() {
// Surface might already be initialized, so we force the call to onSurfaceChanged
surface.setVisibility(View.GONE);
surface.setVisibility(View.VISIBLE);
}

mCameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
cameraService = new CameraService(getActivity());
if (cameraService != null) {
if ((checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) || (checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
Log.d(TAG, "No camera and storage permission");
requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 50404);
}

cameraService.openCamera(deepAr);
private void requestCameraPermissions() {
if ((checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) || (checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
Log.d(TAG, "No camera and storage permission");
requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 50404);
}

}

private Activity getActivity() {
Expand Down Expand Up @@ -178,20 +159,29 @@ private String saveToInternalStorage(Bitmap bitmapImage) {
// Methods
//

public void setLicenseKey(String apiKey) {
if (deepAr == null) {
return;
}

deepAr.setLicenseKey(apiKey);
}

public void switchCamera(int cameraDevice) {
if (cameraService != null) {
if (cameraService == null) {
return ;
}

pause();
pause();

cameraService.switchCamera(cameraDevice);
cameraService.openCamera(deepAr);
cameraService.switchCamera(cameraDevice);
cameraService.openCamera(deepAr);

String status = cameraDevice == Camera.CameraInfo.CAMERA_FACING_FRONT ? "front" : "back";
String status = cameraDevice == Camera.CameraInfo.CAMERA_FACING_FRONT ? "front" : "back";

sendEvent("cameraSwitched", status, null);
sendEvent("cameraSwitched", status, null);

resume();
}
resume();
}

public void switchEffect(String effectName, String slot) {
Expand Down Expand Up @@ -231,14 +221,11 @@ public void fireTrigger(String trigger) {
}

public void setFlashOn(boolean enabled) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String mCameraId = mCameraManager.getCameraIdList()[0];
mCameraManager.setTorchMode(mCameraId, enabled);
}
} catch (Exception e) {
e.printStackTrace();
if (cameraService == null) {
return;
}

cameraService.setFlashOn(enabled);
}

public void pause() {
Expand Down Expand Up @@ -482,6 +469,11 @@ public void surfaceDestroyed(SurfaceHolder holder) {
* */
@Override
public void initialized() {
cameraService = new CameraService(getActivity());

requestCameraPermissions();
cameraService.openCamera(deepAr);

sendEvent("initialized", null, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.widget.FrameLayout;

import androidx.camera.core.CameraSelector;

import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder;
Expand All @@ -21,16 +19,11 @@
import java.util.HashMap;
import java.util.Map;

import ai.deepar.ar.DeepAR;

import javax.annotation.Nullable;

@ReactModule(name = RNTDeepARViewManager.REACT_CLASS)
public class RNTDeepARViewManager extends SimpleViewManager<RNTDeepAR> {

public static final String REACT_CLASS = "RNTDeepARView";
private DeepAR deepAr;
private ReactContext context;

/**
* General
Expand Down Expand Up @@ -82,10 +75,7 @@ public String getName() {

@Override
protected RNTDeepAR createViewInstance(ThemedReactContext reactContext) {
context = reactContext;
deepAr = new DeepAR(reactContext);

return new RNTDeepAR(reactContext, deepAr);
return new RNTDeepAR(reactContext);
}

@Override
Expand Down Expand Up @@ -123,12 +113,12 @@ Map<String, Integer> getCommandsMap() {
}

@ReactProp(name = "apiKey")
public void setApiKey(FrameLayout view, String apiKey) {
deepAr.setLicenseKey(apiKey);
public void setApiKey(RNTDeepAR deepARView, String apiKey) {
deepARView.setLicenseKey(apiKey);
}

@ReactProp(name = "videoWarmup")
public void setVideoWarmup(FrameLayout view, String enabled) {
public void setVideoWarmup(RNTDeepAR deepARView, String enabled) {
// ..
}

Expand Down

0 comments on commit 5a7f830

Please sign in to comment.