0

Good afternoon! Tell me how to overlay an image (ImageView), anchoring the to the eyes? I want to track using Google Vision API. Maybe there is an example of how to put a hat on head?

Added. (12 Jule 2016)

Use the following code:

imageView.setX(mPosition.x);
imageView.setY(mPosition.y);
postInvalidate();

not anchoring (blue square) strictly to the eye, you can see it in the screenshots with different distance from the photos:

enter image description here

Please tell me why this happens?

Wilt
  • 41,477
  • 12
  • 152
  • 203
Jone Moki
  • 21
  • 5

1 Answers1

1

There's a sample in the Google Mobile Vision API GitHub samples that illustrates how to draw eyeball graphics based on the position of the eyes: https://github.com/googlesamples/android-vision/tree/master/visionSamples/googly-eyes

In order to overlay an image instead of drawing primitives (as the sample does), you could load the image into a Bitmap

InputStream inputStream = contentResolver.openInputStream(imageUri);
Bitmap mBitmap = BitmapFactory.decodeStream(inputStream, null, options); 

Then draw it to the canvas at the desired location using canvas.drawBitmap

canvas.drawBitmap(mBitmap, eyePosition.x, eyePosition.y, mPaint);

If you were married to an ImageView, you could load the image into it and then update that position directly when the tracker updates

imageView.setX(mPosition.x);
imageView.setY(mPosition.y);
postInvalidate();

Make sure those set calls happen on the thread that created the View, or they'll throw an exception.

  • theoreticalb Thank you very much for the excellent advice. Variant with "Canvas.drawBitmap (mBitmap, eyePosition.x, eyePosition.y, mPaint);" It works as it should. But with the movement of imageViev remained the same problems. When approximationand out (physically) camera (smartphone) from the face, imageViev not tied strictly to one point (eye) and shifts to the left / right. How can I fix it? – Jone Moki Jul 11 '16 at 21:30
  • I've updated the question. Added new information (details). Look please. – Jone Moki Jul 12 '16 at 20:58
  • You may need to translate the coordinates to account for the positions being in terms of the image rather than the view. The sample has helper methods for this: imageView.setX(translateX(mPosition.x)); imageView.setY(translateY(mPosition.y)); That would put them in the right coordinates for the canvas, which I think would be appropriate if the ImageView is in the same container as the canvas. If not, you may need to also adjust for the offset of the ImageView's container. – theoreticalb Jul 14 '16 at 19:32
  • Unfortunately the offered variants don't help. The solution to "imageView.setX (translateX (mPosition.x)); imageView.setY (translateY (mPosition.y));" doesn't work - imageView even not visible on the screen. Change imageView position in the container, also doesn't give correct results. It seems to me that there exists a bug and I don't know how to solve it? Help me please. – Jone Moki Jul 14 '16 at 20:33
  • If you still have this problem, problem lies in the size of the image change the size of the image make it smaller etc and try again – Ali Asheer Jan 04 '17 at 11:40