-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Android Camera v2 API #339
base: master
Are you sure you want to change the base?
Conversation
… of 2 rather than device pixel ratio. This fixes the offset for devices with pixel ratio of 3 while continuing to work on devices with pixel ratio of 2.
…omLimit startPreview() option
…mit startPreview() option
Hey @dpa99c feel free to propose your PR here: Cap-go/camera-preview#61 |
@dpa99c Is this changes works? Maybe a demo video demonstrating this changes could be good to add in PR description. It could get easy for @pbowyer or @arielhernandezmusa to get quick reviewed. Hope this get merged soon. I am waiting for the Camera2 API to be integrated in the capacitor soon :) |
…s to ensure all forms of exception are caught
Send error and log messages as plugin events back to JS layer
…re no errors due to incorrect escaping
…and do not attempt to restore previous orientation
This is a huge step in the right direction and deserves some attention to get it merged. |
# Conflicts: # android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java
…s to ensure all forms of exception are caught # Conflicts: # android/src/main/java/com/ahm/capacitor/camera/preview/CameraActivity.java # android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java
Send error and log messages as plugin events back to JS layer
…re no errors due to incorrect escaping
…and do not attempt to restore previous orientation # Conflicts: # android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java
…dova version of this plugin. Resolves capacitor-community#99
I am using this test project to validate the plugin functionality: |
@dpa99c Thanks for the test project. I believe that using a square preview in the test project is camouflaging a bug in computing the transformation matrix for the preview, since width and height are often the same and many aspect ratios collapse out to 1. If you make the width, twice the height, then it becomes apparent that, in landscape mode the preview looks stretched vertically, while in portrait the opposite happens (the preview is squashed in Y) I took a look through configureTransform, but to be honest did not understand it very well. It seems likely that scaleY is calculated wrongly, but trying to work out what it should be hurts my brain. I have only tested on Android. I don't have access to an apple device |
return mCameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); | ||
} | ||
|
||
private void configureTransform(int viewWidth, int viewHeight) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still haven't pinned this one down, but something like:
private void configureTransform(int viewWidth, int viewHeight) {
if (cameraDevice == null || !textureView.isAvailable() || mPreviewSize == null) {
return;
}
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
float scale = Math.max(
(float) viewHeight / mPreviewSize.getHeight(),
(float) viewWidth / mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
}
textureView.setTransform(matrix);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have another fix to also include in this PR, so I'll try to make some time today to add this and @eggbeard's fix to the PR. Looks like I also need to resolve conflicts with master so will do that at the same time.
I suggest some exhaustive testing to find any other issues before merging in to master though.
I created this code for a specific use case (e.g. square preview) so have not extensively tested other generic usages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dpa99c thank you very much for the prompt feedback. I will try to do more testing once you add your changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
having trouble getting this working for different preview aspect ratios so it's going to take me a bit longer to get it right
It seems that I faced the same issue while testing this PR with my existing implementation. Here is my preview UI and here is the photo taken/saved In my implementation I use the below camera preview options (note that x, y, width and height are not provided!)
while the test/demo app for this PR uses this https://github.com/dpa99c/capacitor-camera-preview-test/blob/master/src/views/HomePage.vue#L95 camera preview options (note that x, y, width and height are provided!) which makes the preview to be a square with the same width/height. Therefore, I think that, the camera preview works incorrectly when x, y, width and height are not provided. Please note that these are optional (see https://github.com/capacitor-community/camera-preview?tab=readme-ov-file#startoptions) and, I think, that the camera plugin should work as before when these are not provided. |
This PR contains a rewrite of the Android implementation to use the Camera v2 API instead of the deprecated Camera API.
The main reason for this change is to fix issues I was encountering with the existing implementation where the camera preview/captured image from this plugin did not correspond the camera preview/captured image from the default Camera app. i.e. this fixes #275
It also opens the way to fetch the device's camera details and select a specific camera/lens based on these [see #256].
While the fetch side is implemented by this PR (
getCameraCharacteristics()
), the selection of a specific camera/lens is not (as I didn't need it for my use case).I also implements
getZoom()
,setZoom()
andgetMaxZoom()
on both Android & iOS, reworking my previous PR #326 on Android to work with Camera v2 API.