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
🐛 Camera is not aligned properly when using useSkiaFrameProcessor
#2762
Comments
This comment was marked as spam.
This comment was marked as spam.
I will never understand why people ignore the fact that I need log output. I will write a bot in the future to auto-close issues that don't have valid log output. |
Maybe play around with |
useSkiaFrameProcessor
This happened when you using resizeMode={'contain'} Try to delete this from Camera props |
@Space6188 does it properly display without the |
I can also reproduce this when using |
Try playing around with the code from react-native-vision-camera/package/src/skia/useSkiaFrameProcessor.ts Lines 165 to 175 in 3ff02cc
Maybe we're doing something wrong. Also, the |
then you will have zero issues in vc 😅 |
i tried all resizeMode possibilities, also all fit possibilities, the problem is not there |
then it's probably the sensor to buffer transform matrix. |
No,my bad,sorry, it is not working too |
i think that i found a fix for this, should send a pr some time tomorrow |
In case it helps - I am able to reproduce this by passing in the format prop with a custom videoResolution to the camera. Removing the format prop seems to fix the issue. Edit: Actually, removing format only fixes the white bar that appears to the left of the preview, and moves it down to the bottom. Which is less noticeable as it looks like that's just where the camera preview ends. But in my case, the preview should take up the entire screen, but doesn't when using the Skia frame processor. |
it seems that diff --git a/package/src/skia/useSkiaFrameProcessor.ts b/package/src/skia/useSkiaFrameProcessor.ts
index 3feb336..fd69ca1 100644
--- a/package/src/skia/useSkiaFrameProcessor.ts
+++ b/package/src/skia/useSkiaFrameProcessor.ts
@@ -6,8 +6,34 @@ import { VisionCameraProxy, wrapFrameProcessorWithRefCounting } from '../FramePr
import type { DrawableFrameProcessor } from '../types/CameraProps'
import type { ISharedValue, IWorkletNativeApi } from 'react-native-worklets-core'
import { WorkletsProxy } from '../dependencies/WorkletsProxy'
-import type { SkCanvas, SkPaint, SkImage, SkSurface } from '@shopify/react-native-skia'
+import { type SkCanvas, type SkPaint, type SkImage, type SkSurface, ColorType, AlphaType, Skia } from '@shopify/react-native-skia'
import { SkiaProxy } from '../dependencies/SkiaProxy'
+import { InteractionManager } from 'react-native'
+
+// Mocked Frame Data for testings
+const mockedImageWidth = 1920
+const mockedImageHeight = 1080
+
+const pixels = new Uint8Array(mockedImageWidth * mockedImageHeight * 4)
+pixels.fill(255)
+let i = 0
+for (let x = 0; x < mockedImageWidth; x++) {
+ for (let y = 0; y < mockedImageHeight; y++) {
+ pixels[i++] = (x * y) % 255
+ }
+}
+
+const data = Skia.Data.fromBytes(pixels)
+const image = Skia.Image.MakeImage(
+ {
+ width: mockedImageWidth,
+ height: mockedImageHeight,
+ alphaType: AlphaType.Opaque,
+ colorType: ColorType.RGBA_8888,
+ },
+ data,
+ mockedImageWidth * 4,
+)!
/**
* Represents a Camera Frame that can be directly drawn to using Skia.
@@ -155,7 +181,7 @@ export function createSkiaFrameProcessor(
// Convert Frame to SkImage/Texture
const nativeBuffer = (frame as FrameInternal).getNativeBuffer()
- const image = Skia.Image.MakeImageFromNativeBuffer(nativeBuffer.pointer)
+ // const image = Skia.Image.MakeImageFromNativeBuffer(nativeBuffer.pointer)
return new Proxy(frame as DrawableFrame, {
get: (_, property: keyof DrawableFrame) => {
@@ -165,11 +191,36 @@ export function createSkiaFrameProcessor(
'worklet'
// rotate canvas to properly account for Frame orientation
canvas.save()
- const rotation = getRotationDegrees(frame.orientation)
- canvas.rotate(rotation, frame.width / 2, frame.height / 2)
- // render the Camera Frame to the Canvas
- if (paint != null) canvas.drawImage(image, 0, 0, paint)
- else canvas.drawImage(image, 0, 0)
+
+ const rotationAngle = 0 //getRotationDegrees('portrait')
+ const { width: frameWidth, height: frameHeight } = getPortraitSize(frame)
+
+ // center of the canvas
+ const centerX = frameWidth / 2
+ const centerY = frameHeight / 2
+
+ const currentPaint = paint ?? Skia.Paint()
+
+ // only draw the rect by 40% of the frame size so we can vizualize the whole thing to debug :)
+ const reductionFactor = 0.4
+ const rectWidth = frameWidth * reductionFactor
+ const rectHeight = frameHeight * reductionFactor
+
+ // apply rotation around the center of the canvas
+ canvas.rotate(rotationAngle, centerX, centerY)
+
+ const rectX = centerX - rectWidth / 2
+ const rectY = centerY - rectHeight / 2
+
+ const srcRect = Skia.XYWHRect(0, 0, image.width(), image.height())
+ // Define the destination rectangle on the canvas
+ const dstRect = Skia.XYWHRect(rectX, rectY, rectWidth, rectHeight)
+ // Draw the image on the canvas
+
+ canvas.drawImageRect(image, srcRect, dstRect, currentPaint)
// restore transforms/rotations again
canvas.restore()
@@ -185,7 +236,7 @@ export function createSkiaFrameProcessor(
return () => {
'worklet'
// dispose the Frame and the SkImage/Texture
- image.dispose()
+ // image.dispose() // we do not dispose our mocked image :)
nativeBuffer.delete()
}
}
Note I'm doing some tricks to print the preview at 40% of its original size for better debugging However, at the end of the day, this code just simplifies to:
|
Oh wait, this solves the misalignment of the frame inside the canvas; however, it causes another issue: the image buffer is now rotated 90 degrees clockwise inside a (0 deg portrait) preview for me. I think this is expected since we are lacking orientation support for Android, @mrousavy? |
I am applying the orientation inside the |
i mean, Lines 89 to 93 in a53166b
|
no, |
That's because of how Cameras work. Sensors are in 90deg orientation. See |
What's happening?
🐛 [^4.0.0-beta.16] - Camera is not aligned properly when using useSkiaFrameProcessor but it's work with
Reproduceable Code
Relevant log output
Camera Device
none
Device
vivo y21
VisionCamera Version
[^4.0.0-beta.16]
Can you reproduce this issue in the VisionCamera Example app?
I didn't try (⚠️ your issue might get ignored & closed if you don't try this)
Additional information
The text was updated successfully, but these errors were encountered: