Java Code Examples for android.graphics.Matrix#invert()

The following examples show how to use android.graphics.Matrix#invert() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PinchImageView.java    From SmallGdufe-Android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 计算点除以矩阵的值
 *
 * matrix.mapPoints(unknownPoint) -> point
 * 已知point和matrix,求unknownPoint的值.
 *
 * @param point
 * @param matrix
 * @return unknownPoint
 */
public static float[] inverseMatrixPoint(float[] point, Matrix matrix) {
    if (point != null && matrix != null) {
        float[] dst = new float[2];
        //计算matrix的逆矩阵
        Matrix inverse = matrixTake();
        matrix.invert(inverse);
        //用逆矩阵变换point到dst,dst就是结果
        inverse.mapPoints(dst, point);
        //清除临时变量
        matrixGiven(inverse);
        return dst;
    } else {
        return new float[2];
    }
}
 
Example 2
Source File: TflitePlugin.java    From flutter_tflite with MIT License 6 votes vote down vote up
private static Matrix getTransformationMatrix(final int srcWidth,
                                              final int srcHeight,
                                              final int dstWidth,
                                              final int dstHeight,
                                              final boolean maintainAspectRatio) {
  final Matrix matrix = new Matrix();

  if (srcWidth != dstWidth || srcHeight != dstHeight) {
    final float scaleFactorX = dstWidth / (float) srcWidth;
    final float scaleFactorY = dstHeight / (float) srcHeight;

    if (maintainAspectRatio) {
      final float scaleFactor = Math.max(scaleFactorX, scaleFactorY);
      matrix.postScale(scaleFactor, scaleFactor);
    } else {
      matrix.postScale(scaleFactorX, scaleFactorY);
    }
  }

  matrix.invert(new Matrix());
  return matrix;
}
 
Example 3
Source File: Viewport.java    From android-anuto with GNU General Public License v2.0 6 votes vote down vote up
private void calcScreenMatrix() {
    mScreenMatrix = new Matrix();

    float tileSize = Math.min(mScreenWidth / mGameWidth, mScreenHeight / mGameHeight);
    mScreenMatrix.postTranslate(0.5f, 0.5f);
    mScreenMatrix.postScale(tileSize, tileSize);

    float paddingLeft = (mScreenWidth - (tileSize * mGameWidth)) / 2f;
    float paddingBottom = (mScreenHeight - (tileSize * mGameHeight));
    mScreenMatrix.postTranslate(paddingLeft, paddingBottom);

    mScreenMatrix.postScale(1f, -1f);
    mScreenMatrix.postTranslate(0, mScreenHeight);

    mScreenMatrixInverse = new Matrix();
    mScreenMatrix.invert(mScreenMatrixInverse);
}
 
Example 4
Source File: AnimationMatrix.java    From deltachat-android with GNU General Public License v3.0 6 votes vote down vote up
static @NonNull AnimationMatrix animate(@NonNull Matrix from, @NonNull Matrix to, @Nullable Runnable invalidate) {
  if (invalidate == null) {
    return NULL;
  }

  Matrix undo = new Matrix();
  boolean inverted = to.invert(undo);
  if (inverted) {
    undo.preConcat(from);
  }
  if (inverted && !undo.isIdentity()) {
    AnimationMatrix animationMatrix = new AnimationMatrix(undo, invalidate);
    animationMatrix.start(interpolator);
    return animationMatrix;
  } else {
    return NULL;
  }
}
 
Example 5
Source File: PinchImageView.java    From AcgClub with MIT License 6 votes vote down vote up
/**
 * 计算点除以矩阵的值
 *
 * matrix.mapPoints(unknownPoint) -> point
 * 已知point和matrix,求unknownPoint的值.
 *
 * @return unknownPoint
 */
public static float[] inverseMatrixPoint(float[] point, Matrix matrix) {
  if (point != null && matrix != null) {
    float[] dst = new float[2];
    //计算matrix的逆矩阵
    Matrix inverse = matrixTake();
    matrix.invert(inverse);
    //用逆矩阵变换point到dst,dst就是结果
    inverse.mapPoints(dst, point);
    //清除临时变量
    matrixGiven(inverse);
    return dst;
  } else {
    return new float[2];
  }
}
 
Example 6
Source File: GroupElement.java    From cidrawing with Apache License 2.0 6 votes vote down vote up
@Override
public void applyMatrixForData(Matrix matrix) {
    super.applyMatrixForData(matrix);

    for (DrawElement element : elements) {

        // Before applying data matrix, it should transfer the display matrices to data.
        // After that, it should revert back to original display matrix.

        Matrix parentInvertDisplay = new Matrix(getInvertedDisplayMatrix());
        Matrix originalDisplay = new Matrix(element.getDisplayMatrix());
        originalDisplay.postConcat(parentInvertDisplay);
        Matrix originalInvertDisplay = new Matrix();
        originalDisplay.invert(originalInvertDisplay);

        element.applyDisplayMatrixToData();
        element.applyMatrixForData(matrix);
        element.applyMatrixForData(originalInvertDisplay);
        element.getDisplayMatrix().postConcat(originalDisplay);
        element.updateBoundingBox();
    }
    recalculateBoundingBox();
}
 
Example 7
Source File: EditorElementHierarchy.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns a matrix that maps points from the crop on to the visible image.
 * <p>
 * i.e. if a mapped point is in bounds, then the point is on the visible image.
 */
@Nullable Matrix imageMatrixRelativeToCrop() {
  EditorElement mainImage = getMainImage();
  if (mainImage == null) return null;

  Matrix matrix1 = new Matrix(imageCrop.getLocalMatrix());
  matrix1.preConcat(cropEditorElement.getLocalMatrix());
  matrix1.preConcat(cropEditorElement.getEditorMatrix());

  Matrix matrix2 = new Matrix(mainImage.getLocalMatrix());
  matrix2.preConcat(mainImage.getEditorMatrix());
  matrix2.preConcat(imageCrop.getLocalMatrix());

  Matrix inverse = new Matrix();
  matrix2.invert(inverse);
  inverse.preConcat(matrix1);

  return inverse;
}
 
Example 8
Source File: FrescoVitoRegionDecoder.java    From fresco with MIT License 6 votes vote down vote up
private @Nullable Rect computeRegionToDecode(
    EncodedImage encodedImage, ImageDecodeOptions options) {
  if (!(options instanceof FrescoVitoImageDecodeOptions)) {
    return null;
  }
  FrescoVitoImageDecodeOptions frescoVitoOptions = (FrescoVitoImageDecodeOptions) options;

  Rect regionToDecode = new Rect();
  Matrix matrix = new Matrix();
  frescoVitoOptions.scaleType.getTransform(
      matrix,
      frescoVitoOptions.parentBounds,
      encodedImage.getWidth(),
      encodedImage.getHeight(),
      frescoVitoOptions.focusPoint.x,
      frescoVitoOptions.focusPoint.y);
  matrix.invert(matrix);

  RectF tempRectangle = new RectF(frescoVitoOptions.parentBounds);
  matrix.mapRect(tempRectangle);

  tempRectangle.round(regionToDecode);
  return regionToDecode;
}
 
Example 9
Source File: CropMath.java    From imageCrop with MIT License 5 votes vote down vote up
/**
 * Checks if a given point is within a rotated rectangle.
 *
 * @param point 2D point to check
 * @param bound rectangle to rotate
 * @param rot angle of rotation about rectangle center
 * @return true if point is within rotated rectangle
 */
public static boolean pointInRotatedRect(float[] point, RectF bound, float rot) {
    Matrix m = new Matrix();
    float[] p = Arrays.copyOf(point, 2);
    m.setRotate(rot, bound.centerX(), bound.centerY());
    Matrix m0 = new Matrix();
    if (!m.invert(m0))
        return false;
    m0.mapPoints(p);
    return inclusiveContains(bound, p[0], p[1]);
}
 
Example 10
Source File: EditorModel.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
private @Nullable Matrix findElementMatrix(@NonNull EditorElement element, @NonNull Matrix viewMatrix) {
  Matrix inverse = findElementInverseMatrix(element, viewMatrix);
  if (inverse != null) {
    Matrix regular = new Matrix();
    inverse.invert(regular);
    return regular;
  }
  return null;
}
 
Example 11
Source File: ElementGroup.java    From cidrawing with Apache License 2.0 5 votes vote down vote up
@Override
public void restoreDisplayMatrixFromData(Matrix matrix) {
    if (matrix != null) {
        Matrix invertDisplayMatrix = new Matrix();
        matrix.invert(invertDisplayMatrix);
        applyMatrixForData(invertDisplayMatrix);
        getDisplayMatrix().set(new Matrix(matrix));
        recalculateBoundingBox();
    }
}
 
Example 12
Source File: OpenCamera.java    From CameraBlur with Apache License 2.0 5 votes vote down vote up
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int width, int height) {
    refreshCamera();
    Matrix matrix = new Matrix();

    matrix.postScale(width / 2000f, height / 2000f);
    matrix.postTranslate(width / 2f, height / 2f);
    matrix.invert(this.matrix);
}
 
Example 13
Source File: MatrixView.java    From zone-sdk with MIT License 5 votes vote down vote up
@Override
	protected void onDraw(Canvas canvas) {
//		super.onDraw(canvas);
		Matrix matrix=new Matrix();
		matrix.postTranslate(100, 200);
		matrix.mapPoints(dst, src);
		System.out.println("mapPoints_____>>>src first Point X:" + src[0] + "\t  Y:" + src[1]);//src是记录的原点  [0,0]点
		System.out.println("mapPoints_____>>>dst first Point X:"+ dst[0]+"\t  Y:"+dst[1]);//dst是 src通过矩阵变换的点 [0,0]点对应到 [100,200]
		canvas.drawBitmap(bt, matrix, paint);

		//通过下边的三个方法  可以通过  dst矩阵变化后的点  通过矩阵 逆向方法 转换到 invertDst  即是src原点位置   所以dst中[100,200]的点 即是inverDst[0,0]这个点即 src[0,0]点
		Matrix invertMatrix = new Matrix();
		matrix.invert(invertMatrix);
		invertMatrix.mapPoints(invertDst, dst);
		System.out.println("invertMatrix_____>>>mapPoints  invertDst first Point X:" + invertDst[0] + "\t  Y:" + invertDst[1]);

		//测试postConcat
		Matrix matrixCon =new Matrix();
		matrixCon.postTranslate(100, 200);
		matrix.postConcat(matrixCon);
		matrix.mapPoints(dst, src);
		System.out.println("matrixCon:  mapPoints_____>>>src first Point X:" + src[0] + "\t  Y:" + src[1]);//src是记录的原点  [0,0]点
		System.out.println("matrixCon:  mapPoints_____>>>dst first Point X:"+ dst[0]+"\t  Y:"+dst[1]);


		//测试Camera getMatrix
		Camera camera=new Camera();
		camera.save();
		camera.translate(100,200,0);
		camera.getMatrix(matrix);
		camera.restore();
		matrix.mapPoints(dst, src);
		System.out.println("Camera:  mapPoints_____>>>src first Point X:" + src[0] + "\t  Y:" + src[1]);//src是记录的原点  [0,0]点
		System.out.println("Camera:  mapPoints_____>>>dst first Point X:"+ dst[0]+"\t  Y:"+dst[1]);

	}
 
Example 14
Source File: CropImageView.java    From CVScanner with GNU General Public License v3.0 5 votes vote down vote up
private float[] mapPointToImageSpace(float x, float y) {
    float[] p = new float[2];
    Matrix m = getImageViewMatrix();
    Matrix m2 = new Matrix();
    m.invert(m2);
    p[0] = x;
    p[1] = y;
    m2.mapPoints(p);
    return p;
}
 
Example 15
Source File: MainActivity.java    From yolov3-android-tflite with MIT License 4 votes vote down vote up
public static Bitmap processBitmap(Bitmap source,int size){

        int image_height = source.getHeight();
        int image_width = source.getWidth();

        Bitmap croppedBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

        Matrix frameToCropTransformations = getTransformationMatrix(image_width,image_height,size,size,0,false);
        Matrix cropToFrameTransformations = new Matrix();
        frameToCropTransformations.invert(cropToFrameTransformations);

        final Canvas canvas = new Canvas(croppedBitmap);
        canvas.drawBitmap(source, frameToCropTransformations, null);

        return croppedBitmap;


    }
 
Example 16
Source File: CameraView.java    From KrGallery with GNU General Public License v2.0 4 votes vote down vote up
private void adjustAspectRatio(int previewWidth, int previewHeight, int rotation) {
    txform.reset();

    int viewWidth = getWidth();
    int viewHeight = getHeight();
    float viewCenterX = viewWidth / 2;
    float viewCenterY = viewHeight / 2;

    float scale;
    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
        scale = Math.max((float) (viewHeight + clipTop) / previewWidth, (float) (viewWidth + clipLeft) / previewHeight);
    } else {
        scale = Math.max((float) (viewHeight + clipTop) / previewHeight, (float) (viewWidth + clipLeft) / previewWidth);
    }

    float previewWidthScaled = previewWidth * scale;
    float previewHeightScaled = previewHeight * scale;

    float scaleX = previewHeightScaled / (viewWidth);
    float scaleY = previewWidthScaled / (viewHeight);

    txform.postScale(scaleX, scaleY, viewCenterX, viewCenterY);

    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        txform.postRotate(90 * (rotation - 2), viewCenterX, viewCenterY);
    } else {
        if (Surface.ROTATION_180 == rotation) {
            txform.postRotate(180, viewCenterX, viewCenterY);
        }
    }

    if (mirror) {
        txform.postScale(-1, 1, viewCenterX, viewCenterY);
    }
    if (clipTop != 0 || clipLeft != 0) {
        txform.postTranslate(-clipLeft / 2, -clipTop / 2);
    }

    textureView.setTransform(txform);

    Matrix matrix = new Matrix();
    matrix.postRotate(cameraSession.getDisplayOrientation());
    matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
    matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
    matrix.invert(this.matrix);
}
 
Example 17
Source File: Input.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public void setMatrix(Matrix m) {
    invertMatrix = new Matrix();
    m.invert(invertMatrix);
}
 
Example 18
Source File: CameraView.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private void adjustAspectRatio(int previewWidth, int previewHeight, int rotation) {
    txform.reset();

    int viewWidth = getWidth();
    int viewHeight = getHeight();
    float viewCenterX = viewWidth / 2;
    float viewCenterY = viewHeight / 2;

    float scale;
    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
        scale = Math.max((float) (viewHeight + clipTop + clipBottom) / previewWidth, (float) (viewWidth) / previewHeight);
    } else {
        scale = Math.max((float) (viewHeight + clipTop + clipBottom) / previewHeight, (float) (viewWidth) / previewWidth);
    }

    float previewWidthScaled = previewWidth * scale;
    float previewHeightScaled = previewHeight * scale;

    float scaleX = previewHeightScaled / (viewWidth);
    float scaleY = previewWidthScaled / (viewHeight);

    txform.postScale(scaleX, scaleY, viewCenterX, viewCenterY);

    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        txform.postRotate(90 * (rotation - 2), viewCenterX, viewCenterY);
    } else {
        if (Surface.ROTATION_180 == rotation) {
            txform.postRotate(180, viewCenterX, viewCenterY);
        }
    }

    if (mirror) {
        txform.postScale(-1, 1, viewCenterX, viewCenterY);
    }
    if (clipTop != 0) {
        txform.postTranslate(0, -clipTop / 2);
    } else if (clipBottom != 0) {
        txform.postTranslate(0, clipBottom / 2);
    }

    textureView.setTransform(txform);

    Matrix matrix = new Matrix();
    if (cameraSession != null) {
        matrix.postRotate(cameraSession.getDisplayOrientation());
    }
    matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
    matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
    matrix.invert(this.matrix);
}
 
Example 19
Source File: CameraCoordinateTransformer.java    From Camera2 with Apache License 2.0 4 votes vote down vote up
private Matrix inverse(Matrix source)
{
    Matrix newMatrix = new Matrix();
    source.invert(newMatrix);
    return newMatrix;
}
 
Example 20
Source File: CameraView.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void adjustAspectRatio(int previewWidth, int previewHeight, int rotation) {
    txform.reset();

    int viewWidth = getWidth();
    int viewHeight = getHeight();
    float viewCenterX = viewWidth / 2;
    float viewCenterY = viewHeight / 2;

    float scale;
    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
        scale = Math.max((float) (viewHeight + clipTop) / previewWidth, (float) (viewWidth + clipLeft) / previewHeight);
    } else {
        scale = Math.max((float) (viewHeight + clipTop) / previewHeight, (float) (viewWidth + clipLeft) / previewWidth);
    }

    float previewWidthScaled = previewWidth * scale;
    float previewHeightScaled = previewHeight * scale;

    float scaleX = previewHeightScaled / (viewWidth);
    float scaleY = previewWidthScaled / (viewHeight);

    txform.postScale(scaleX, scaleY, viewCenterX, viewCenterY);

    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        txform.postRotate(90 * (rotation - 2), viewCenterX, viewCenterY);
    } else {
        if (Surface.ROTATION_180 == rotation) {
            txform.postRotate(180, viewCenterX, viewCenterY);
        }
    }

    if (mirror) {
        txform.postScale(-1, 1, viewCenterX, viewCenterY);
    }
    if (clipTop != 0 || clipLeft != 0) {
        txform.postTranslate(-clipLeft / 2, -clipTop / 2);
    }

    textureView.setTransform(txform);

    Matrix matrix = new Matrix();
    matrix.postRotate(cameraSession.getDisplayOrientation());
    matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
    matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
    matrix.invert(this.matrix);
}