Skip to content

Commit 224cbbf

Browse files
fix: drawing style transfer image
1 parent 2c8dd67 commit 224cbbf

File tree

1 file changed

+23
-20
lines changed
  • apps/computer-vision/app/style_transfer

1 file changed

+23
-20
lines changed

apps/computer-vision/app/style_transfer/index.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default function StyleTransferScreen() {
2727

2828
const [imageUri, setImageUri] = useState('');
2929
const [styledImage, setStyledImage] = useState<SkImage | null>(null);
30+
const [canvasSize, setCanvasSize] = useState({ width: 1, height: 1 });
3031

3132
const handleCameraPress = async (isCamera: boolean) => {
3233
const image = await getImage(isCamera);
@@ -43,16 +44,8 @@ export default function StyleTransferScreen() {
4344
const output = await model.forward(imageUri);
4445
const height = output.sizes[0];
4546
const width = output.sizes[1];
46-
// Convert RGB -> RGBA for Skia
47-
const rgba = new Uint8Array(width * height * 4);
48-
const rgb = output.dataPtr;
49-
for (let i = 0; i < width * height; i++) {
50-
rgba[i * 4] = rgb[i * 3];
51-
rgba[i * 4 + 1] = rgb[i * 3 + 1];
52-
rgba[i * 4 + 2] = rgb[i * 3 + 2];
53-
rgba[i * 4 + 3] = 255;
54-
}
55-
const skData = Skia.Data.fromBytes(rgba);
47+
// Native already returns RGBA uint8 — use directly
48+
const skData = Skia.Data.fromBytes(output.dataPtr);
5649
const img = Skia.Image.MakeImage(
5750
{
5851
width,
@@ -83,16 +76,26 @@ export default function StyleTransferScreen() {
8376
<ScreenWrapper>
8477
<View style={styles.imageContainer}>
8578
{styledImage ? (
86-
<Canvas style={styles.canvas}>
87-
<SkiaImage
88-
image={styledImage}
89-
fit="contain"
90-
x={0}
91-
y={0}
92-
width={styledImage.width()}
93-
height={styledImage.height()}
94-
/>
95-
</Canvas>
79+
<View
80+
style={styles.canvas}
81+
onLayout={(e) =>
82+
setCanvasSize({
83+
width: e.nativeEvent.layout.width,
84+
height: e.nativeEvent.layout.height,
85+
})
86+
}
87+
>
88+
<Canvas style={StyleSheet.absoluteFill}>
89+
<SkiaImage
90+
image={styledImage}
91+
fit="contain"
92+
x={0}
93+
y={0}
94+
width={canvasSize.width}
95+
height={canvasSize.height}
96+
/>
97+
</Canvas>
98+
</View>
9699
) : (
97100
<Image
98101
style={styles.image}

0 commit comments

Comments
 (0)