问题
I see many mobile apps having a feature that user can draw a square to indicate something to tag on the image.
I'm building Face Tagging
app and basically user draws square on the boundary of human's face on the image.
I've Googled it many times but I'm not sure RN has some feature library for tagging.
How can I achieve this? Is there any good library to draw square on image? And it will be even better if I can remember its coordinate width, height, and position of the rectangle.
Any idea or suggestion will be highly appreciated!
This is an example below
回答1:
You can use React Native ART library to draw shapes on top of image. It is a standard library that ships with React Native (although not linked to the binary by default).
Regarding the algorithm:
- Render an image
- Overlay image with React Native's
ART.Surface
- Detect taps to get coordinates + overlay the rest of the image
- Once you have coordinates of the tap, you can draw a shape you want
- Stop drawing shape when user removes his finger (
onPressOut
event)
Where to go from here:
- Basic tutorial, explaining how to wire up React Native and ART
- Unofficial(there no official one) documentation
回答2:
You can add children (in your case, square Views) to an Image tag, so you could do something like
<Image src={...}>
<View
style={{
position: 'absolute',
top: 120
left: 100,
height: 50,
width: 50,
borderWidth: 1
}}
/>
</Image>
You can get the x and y coordinates with the PanResponder API instead of hardcoding the top and left style properties
Edit: RN 50=< Removed support of nested content inside , use ImageBackground instead
来源:https://stackoverflow.com/questions/46864293/how-to-draw-square-to-tag-an-object-react-native