|
1 | 1 | <script lang="typescript"> |
2 | | - import { View } from '@nativescript/core'; |
| 2 | + import { View, ImageSource } from '@nativescript/core'; |
3 | 3 | import { NativeViewElementNode } from 'svelte-native/dom'; |
4 | 4 | import { goBack } from 'svelte-native'; |
5 | 5 | import { CameraView } from '@nativescript-community/ui-cameraview'; |
6 | 6 | let cropView: NativeViewElementNode<View>; |
7 | 7 | let cameraView: NativeViewElementNode<CameraView>; |
8 | 8 | let flashMode = 'off'; |
| 9 | + let imageSource: ImageSource; |
9 | 10 | async function applyProcessor() { |
10 | 11 | try { |
11 | 12 | if (__ANDROID__) { |
|
39 | 40 | } |
40 | 41 |
|
41 | 42 | async function takePicture() { |
42 | | - const start = Date.now() |
| 43 | + console.log('takePicture') |
| 44 | + try { |
| 45 | + const start = Date.now() |
43 | 46 | const {image, info} = await cameraView.nativeView.takePicture({savePhotoToDisk:false}) |
| 47 | + if (imageSource) { |
| 48 | + if(__ANDROID__) { |
| 49 | + imageSource.android.recycle() |
| 50 | + } |
| 51 | + } |
44 | 52 | console.log('onpicture', image, info, Date.now() -start, 'ms') |
45 | | - image.recycle() |
| 53 | + imageSource = new ImageSource(image) |
| 54 | + } catch (error) { |
| 55 | + console.error(error) |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + } |
| 60 | +
|
| 61 | + function onCameraTap(event) { |
| 62 | + cameraView.nativeView.focusAtPoint(event.getX(), event.getY()); |
| 63 | + } |
| 64 | + function toggleCamera(event) { |
| 65 | + cameraView.nativeView.toggleCamera(); |
46 | 66 | } |
47 | 67 | </script> |
48 | 68 |
|
49 | 69 | <page> |
50 | 70 | <actionBar title="Basic CameraView"> |
51 | 71 | <navigationButton text="Go back" on:tap={() => goBack()} /> |
52 | 72 | </actionBar> |
53 | | - <cameraview bind:this={cameraView} {flashMode} captureMode={1}> |
| 73 | + <cameraview bind:this={cameraView} {flashMode} captureMode={1} on:tap={onCameraTap}> |
54 | 74 | <cropview bind:this={cropView} /> |
| 75 | + <image src={imageSource} width={100} verticalAlignment="bottom" horizontalAlignment="left" marginBottom={60} backgroundColor="red"/> |
| 76 | + <button text="toggleCamera" on:tap={toggleCamera} verticalAlignment="top" horizontalAlignment="right" marginTop={60}/> |
55 | 77 | <button text="picture" on:tap={takePicture} verticalAlignment="bottom" horizontalAlignment="right"/> |
56 | 78 | <button text="test processor" on:tap={applyProcessor} verticalAlignment="bottom" horizontalAlignment="left"/> |
57 | 79 | <button text={flashMode} on:tap={switchFlashMode} verticalAlignment="top" horizontalAlignment="right" /></cameraview |
|
0 commit comments