Crop video before encoding with MediaCodec for Grafika’s “Continuous Capture” Activity

Take a look at the “texture from camera” activity. Note it allows you to manipulate the image in various ways, notably “zoom”. The “zoom” is done by modifying the texture coordinates.

The ScaledDrawable2D class does this; the setScale() call changes the “zoom”, rather than scaling the rect itself. Texture coordinates range from 0.0 to 1.0 inclusive, and the getTexCoordArray() method modifies them to span a subset of the texture.

To clip the frames, you’d need to modify the texture coordinates proportionally. For example, if the input video is portrait 720×1280, and you want 720×720, you would change the coordinates from this:

[0.0, 0.0]  [1.0, 0.0]
[0.0, 1.0]  [1.0, 1.0]

to this:

[0.0, 280/1280.0]  [1.0, 280/1280.0]
[0.0, 1000/1280.0] [1.0, 1000/1280.0]

and then render that on a square rather than a rectangle.

Leave a Comment