Stretch a bitmap in a specific corner

Is it possible to stretch a bitmap image in a certain corner? The figure below shows my intention:

enter image description here

I take a picture with a camera, detect the corners of the image and want to convert the contents. As far as I know, this cannot be achieved using the Matrix class. The Camera class should help, but I will need to calculate the position of the camera. Is there an algorithm for this? How do you do this?

+4
source share
2 answers

You made me look into this very interesting problem, and it seems to be easy to do on Android. Use absolute coordinates for four grid points:

float[] mVerts = { topLeftX, topLeftY, topRightX, topRightY, bottomLeftX, bottomLeftY, bottomRightX, bottomRightY }; canvas.drawBitmapMesh(myImage, 1, 1, mVerts, 0, null, 0, null); 

You will need to figure out how to get these points, but drawBitmapMesh will stretch it for you.

+4
source

I think the easiest way to do such a conversion on Android is to use OpenGL. You can treat the bitmap as a texture. You can then use the detected angles as texture coordinates. Assign each of them as a texture coordinate to the corresponding vertex of a simple rectangular shape. Then ask OpenGL to draw it on your canvas. Pseudocode:

 glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, pictureId); glBegin(GL_QUAD); glVertex(0,0); // left upper corner glTexCoord(corners[0].x/picture.getWidth(), corners[0].y/picture.getHeight()); glVertex(1,0); // right upper corner glTexCoord(corners[1].x/picture.getWidth(), corners[1].y/picture.getHeight()); glVertex(1,1); // right lower corner glTexCoord(corners[2].x/picture.getWidth(), corners[2].y/picture.getHeight()); glVertex(0,1); // left lower corner glTexCoord(corners[3].x/picture.getWidth(), corners[3].y/picture.getHeight()); glEnd(); 

You do not need a camera or complex conversions. Of course, this is not very convenient, since using OpenGL for such a simple task is an unnecessary question. But there really is no simpler way than to write such texturing yourself. If you want, you can start reading from the wiki and go to external links and see, for example, the software implementation of texturing methods:

http://en.wikipedia.org/wiki/Texture_mapping

You can also try using any of the OpenGL effect representations, this will simplify the setup, but also ask you to create a shader or two:

http://code.google.com/p/effect-view/

0
source

Source: https://habr.com/ru/post/1468991/


All Articles