Skip to content

Instantly share code, notes, and snippets.

@oak-tree
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save oak-tree/cb75f5dea0b245daef34 to your computer and use it in GitHub Desktop.

Select an option

Save oak-tree/cb75f5dea0b245daef34 to your computer and use it in GitHub Desktop.
mapping grid to preview
//new idea to calculate instead of the *FIRST setRectToRect & Translation*
//note 1: We do not want to resize the grid - the grid window in his original size
//note 2: We acully just want to move the grid from his left top corner to the center of the preivew
//note 3: The **SECOND** setRectToRect should be keept because this is the mapping between the *preview* and the *real image*
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data, INT_START, data.length);
RectF previewRect = new RectF(0, 0, previewWidth, previewHeight);
RectF pictureRect = new RectF(0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight());
//RectF nativeResRect = new RectF(0, 0, screenWidth, screenHeight); // we actully dont care about the native res because we have the grid coordiantes
RectF resultRect = new RectF(0, gridTop, gridRight, gridBottom);
final Matrix scaleMatrix = new Matrix();
scaleMatrix.setTranslate((previewRect.width()-resultRect.width)/2, (previewRect.height()-resultRect.height())/2);
scaleMatrix.mapRect(resultRect); // This will move the the grid to the right position on the preview coordiantes
//dont forget to do the rest of the code (line 129 i.e scaleMatrix.setRectToRect(previewRect, pictureRect, Matrix.ScaleToFit.CENTER))
/*.
.
.
. */
//This is the current code
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data, INT_START, data.length);
RectF previewRect = new RectF(0, 0, previewWidth, previewHeight);
RectF pictureRect = new RectF(0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight());
RectF nativeResRect = new RectF(0, 0, screenWidth, screenHeight);
RectF resultRect = new RectF(0, gridTop, gridRight, gridBottom);
final Matrix scaleMatrix = new Matrix();
scaleMatrix.setRectToRect(nativeResRect, previewRect, Matrix.ScaleToFit.CENTER);
float xTranslate = (Math.abs(previewRect.width() - nativeResRect.width())) / 2;
float yTranslate = 0;
float yTranslateShouldBe = (previewRect.height() - nativeResRect.height()) / 2;
yTranslate = Math.abs(yTranslateShouldBe);
Log.v(CustomCameraPreview.TAG, "native to preview = Matrix: " + scaleMatrix.toString());
scaleMatrix.postTranslate(
xTranslate,
yTranslate
);
Log.v(CustomCameraPreview.TAG, "native to preview + translate = Matrix: " + scaleMatrix.toString());
Log.v(CustomCameraPreview.TAG, "Translate is x: " + xTranslate + ", " +
"y: " + yTranslate + ", yTranslateShouldBe: " + yTranslateShouldBe);
scaleMatrix.mapRect(resultRect);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment