How to draw square to tag an object. (React Native)

白昼怎懂夜的黑 提交于 2019-11-29 15:43:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!