Skip to content

Commit

Permalink
fix(android): fix Android camera with overlay issues (#13913)
Browse files Browse the repository at this point in the history
* fix(android): fix Android camera with overlay issues

* docs: readme
  • Loading branch information
m1ga authored and hansemannn committed Sep 23, 2023
1 parent 951877b commit d56de28
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
Expand Up @@ -93,7 +93,8 @@ public class MediaModule extends KrollModule implements Handler.Callback
public static final int NO_CAMERA = 2;
@Kroll.constant
public static final int NO_VIDEO = 3;

@Kroll.constant
public static final int NO_FOCUS = 4;
@Kroll.constant
public static final int IMAGE_SCALING_AUTO = -1;
@Kroll.constant
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.io.TiContentFile;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.view.TiUIView;

import android.Manifest;
import android.app.Activity;
Expand Down Expand Up @@ -246,14 +247,23 @@ protected void onResume()
cameraActivity = this;
previewLayout.addView(preview,
new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
View overlayView = localOverlayProxy.getOrCreateView().getNativeView();
ViewGroup parent = (ViewGroup) overlayView.getParent();
// Detach from the parent if applicable
if (parent != null) {
parent.removeView(overlayView);
}
cameraLayout.addView(overlayView,
new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
if (localOverlayProxy != null) {
TiUIView localView = localOverlayProxy.getOrCreateView();
if (localView != null) {
View overlayView = localView.getNativeView();
ViewGroup parent = (ViewGroup) overlayView.getParent();
// Detach from the parent if applicable
if (parent != null) {
parent.removeView(overlayView);
}
cameraLayout.addView(overlayView,
new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} else {
Log.e(TAG, "Overlay view is null");
}
} else {
Log.e(TAG, "Overlay is null");
}
}

public static void setFlashMode(int cameraFlashMode)
Expand Down Expand Up @@ -729,6 +739,18 @@ public void onAutoFocus(boolean success, Camera camera)
}
} else {
Log.w(TAG, "Unable to focus.");
if (errorCallback != null) {
KrollDict response = new KrollDict();
response.putCodeAndMessage(MediaModule.NO_FOCUS, "Couldn't focus");
errorCallback.callAsync(callbackContext, response);
}
try {
camera.cancelAutoFocus();
camera.autoFocus(null);
} catch (Exception e) {
Log.w(TAG, "Failed to cancel auto focus: " + e.toString());
}
takingPicture = false;
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion apidoc/Titanium/Media/Media.yml
Expand Up @@ -1228,6 +1228,13 @@ properties:
type: Number
permission: read-only

- name: NO_FOCUS
summary: Constant for camera didn't focus when taking a trying to take a picture.
type: Number
permission: read-only
since:
android: "12.2.1"

- name: VIDEO_CONTROL_DEFAULT
deprecated:
since: "7.0.0"
Expand Down Expand Up @@ -2370,7 +2377,7 @@ properties:
- name: code
summary: Error code, if applicable.
type: Number
constants: [Titanium.Media.DEVICE_BUSY, Titanium.Media.NO_CAMERA, Titanium.Media.UNKNOWN_ERROR]
constants: [Titanium.Media.DEVICE_BUSY, Titanium.Media.NO_CAMERA, Titanium.Media.UNKNOWN_ERROR, Titanium.Media.NO_FOCUS]
accessors: false

- name: success
Expand Down

0 comments on commit d56de28

Please sign in to comment.