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

The following examples show how to use android.graphics.Matrix#postRotate() . 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: FlipLoadingLayout.java    From Social with Apache License 2.0 6 votes vote down vote up
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable) {
	if (null != imageDrawable) {
		final int dHeight = imageDrawable.getIntrinsicHeight();
		final int dWidth = imageDrawable.getIntrinsicWidth();

		/**
		 * We need to set the width/height of the ImageView so that it is
		 * square with each side the size of the largest drawable dimension.
		 * This is so that it doesn't clip when rotated.
		 */
		ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
		lp.width = lp.height = Math.max(dHeight, dWidth);
		mHeaderImage.requestLayout();

		/**
		 * We now rotate the Drawable so that is at the correct rotation,
		 * and is centered.
		 */
		mHeaderImage.setScaleType(ScaleType.MATRIX);
		Matrix matrix = new Matrix();
		matrix.postTranslate((lp.width - dWidth) / 2f, (lp.height - dHeight) / 2f);
		matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f, lp.height / 2f);
		mHeaderImage.setImageMatrix(matrix);
	}
}
 
Example 2
Source File: Camera2BasicFragment.java    From Cam2Caption with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
 * This method should be called after the camera preview size is determined in
 * setUpCameraOutputs and also the size of `mTextureView` is fixed.
 *
 * @param viewWidth  The width of `mTextureView`
 * @param viewHeight The height of `mTextureView`
 */
private void configureTransform(int viewWidth, int viewHeight) {
    Activity activity = getActivity();
    if (null == mTextureView || null == mPreviewSize || null == activity) {
        return;
    }
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    Matrix matrix = new Matrix();
    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
    RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
    float centerX = viewRect.centerX();
    float centerY = viewRect.centerY();
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
        matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
        float scale = Math.max(
                (float) viewHeight / mPreviewSize.getHeight(),
                (float) viewWidth / mPreviewSize.getWidth());
        matrix.postScale(scale, scale, centerX, centerY);
        matrix.postRotate(90 * (rotation - 2), centerX, centerY);
    } else if (Surface.ROTATION_180 == rotation) {
        matrix.postRotate(180, centerX, centerY);
    }
    mTextureView.setTransform(matrix);
}
 
Example 3
Source File: ImageUtils.java    From LLApp with Apache License 2.0 6 votes vote down vote up
/*** 将图片按照某个角度进行旋转
** @param bm
*            需要旋转的图片
* @param degree
*            旋转角度
* @return 旋转后的图片
*/

public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
	Bitmap returnBm = null;
	// 根据旋转角度,生成旋转矩阵
	Matrix matrix = new Matrix();
	matrix.postRotate(degree);
	try {
		// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
		returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
	} catch (OutOfMemoryError e) {
	}
	if (returnBm == null) {
		returnBm = bm;
	}
	if (bm != returnBm) {
		bm.recycle();
	}
	return returnBm;
}
 
Example 4
Source File: CameraConnectionFragment.java    From tensorflow-classifier-android with Apache License 2.0 6 votes vote down vote up
/**
 * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
 * This method should be called after the camera preview size is determined in
 * setUpCameraOutputs and also the size of `mTextureView` is fixed.
 *
 * @param viewWidth  The width of `mTextureView`
 * @param viewHeight The height of `mTextureView`
 */
private void configureTransform(final int viewWidth, final int viewHeight) {
  final Activity activity = getActivity();
  if (null == textureView || null == previewSize || null == activity) {
    return;
  }
  final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
  final Matrix matrix = new Matrix();
  final RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
  final RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
  final float centerX = viewRect.centerX();
  final float centerY = viewRect.centerY();
  if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
    bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
    matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
    final float scale =
        Math.max(
            (float) viewHeight / previewSize.getHeight(),
            (float) viewWidth / previewSize.getWidth());
    matrix.postScale(scale, scale, centerX, centerY);
    matrix.postRotate(90 * (rotation - 2), centerX, centerY);
  } else if (Surface.ROTATION_180 == rotation) {
    matrix.postRotate(180, centerX, centerY);
  }
  textureView.setTransform(matrix);
}
 
Example 5
Source File: CameraConnectionFragment.java    From fritz-examples with MIT License 6 votes vote down vote up
/**
 * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
 * This method should be called after the camera preview size is determined in
 * setUpCameraOutputs and also the size of `mTextureView` is fixed.
 *
 * @param viewWidth  The width of `mTextureView`
 * @param viewHeight The height of `mTextureView`
 */
private void configureTransform(final int viewWidth, final int viewHeight) {
    final Activity activity = getActivity();
    if (null == textureView || null == previewSize || null == activity) {
        return;
    }
    final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    final Matrix matrix = new Matrix();
    final RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
    final RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
    final float centerX = viewRect.centerX();
    final float centerY = viewRect.centerY();
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
        matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
        final float scale =
                Math.max(
                        (float) viewHeight / previewSize.getHeight(),
                        (float) viewWidth / previewSize.getWidth());
        matrix.postScale(scale, scale, centerX, centerY);
        matrix.postRotate(90 * (rotation - 2), centerX, centerY);
    } else if (Surface.ROTATION_180 == rotation) {
        matrix.postRotate(180, centerX, centerY);
    }
    textureView.setTransform(matrix);
}
 
Example 6
Source File: CommonUtil.java    From Pic2Ascii with MIT License 6 votes vote down vote up
/**
 * 旋转图片
 *
 * @param angle  被旋转角度
 * @param bitmap 图片对象
 * @return 旋转后的图片
 */
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
    Bitmap returnBm = null;
    // 根据旋转角度,生成旋转矩阵
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    try {
        // 将原始图片按照旋转矩阵进行旋转,并得到新的图片
        returnBm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } catch (OutOfMemoryError e) {
    }
    if (returnBm == null) {
        returnBm = bitmap;
    }
    if (bitmap != returnBm) {
        bitmap.recycle();
    }
    return returnBm;
}
 
Example 7
Source File: BitmapHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定した角度回転させたBitmapを生成して返す
 * @param bitmap
 * @param rotation
 * @return
 */
@Nullable
public static Bitmap rotateBitmap(
	@Nullable final Bitmap bitmap, final int rotation) {

	if (DEBUG) Log.v(TAG, String.format("rotateBitmap:%d", rotation));
	Bitmap newBitmap = null;
	if (bitmap != null) {
		final int width = bitmap.getWidth();
		final int height = bitmap.getHeight();
		final Matrix matrix = new Matrix();
		matrix.postRotate(rotation);
		newBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
	}
	if (DEBUG) Log.v(TAG, "rotateBitmap:bitmap=" + bitmap + " newBitmap=" + newBitmap);
	return newBitmap;
}
 
Example 8
Source File: GPUImage.java    From SimpleVideoEditor with Apache License 2.0 6 votes vote down vote up
private Bitmap rotateImage(final Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }
    Bitmap rotatedBitmap = bitmap;
    try {
        int orientation = getImageOrientation();
        if (orientation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);
            rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), matrix, true);
            bitmap.recycle();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return rotatedBitmap;
}
 
Example 9
Source File: FlipLoadingLayout.java    From sctalk with Apache License 2.0 6 votes vote down vote up
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable) {
	if (null != imageDrawable) {
		final int dHeight = imageDrawable.getIntrinsicHeight();
		final int dWidth = imageDrawable.getIntrinsicWidth();

		/**
		 * We need to set the width/height of the ImageView so that it is
		 * square with each side the size of the largest drawable dimension.
		 * This is so that it doesn't clip when rotated.
		 */
		ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
		lp.width = lp.height = Math.max(dHeight, dWidth);
		mHeaderImage.requestLayout();

		/**
		 * We now rotate the Drawable so that is at the correct rotation,
		 * and is centered.
		 */
		mHeaderImage.setScaleType(ScaleType.MATRIX);
		Matrix matrix = new Matrix();
		matrix.postTranslate((lp.width - dWidth) / 2f, (lp.height - dHeight) / 2f);
		matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f, lp.height / 2f);
		mHeaderImage.setImageMatrix(matrix);
	}
}
 
Example 10
Source File: CubeMapView.java    From ShaderEditor with MIT License 6 votes vote down vote up
private static Matrix getMatrixFromClip(
		int width,
		int height,
		RectF bounds,
		RectF normalizedClip,
		float rotation) {
	Matrix matrix = new Matrix();
	RectF dstRect = new RectF();

	matrix.setTranslate(width * -.5f, height * -.5f);
	matrix.postRotate(rotation);
	matrix.mapRect(dstRect, new RectF(0f, 0f, width, height));

	float dw = dstRect.width();
	float dh = dstRect.height();
	float scale = bounds.width() / (normalizedClip.width() * dw);
	matrix.postScale(scale, scale);
	matrix.postTranslate(
			bounds.centerX() -
					(normalizedClip.centerX() - .5f) * dw * scale,
			bounds.centerY() -
					(normalizedClip.centerY() - .5f) * dh * scale);

	return matrix;
}
 
Example 11
Source File: MyBitmapUtils.java    From FrescoUtlis with Apache License 2.0 5 votes vote down vote up
/**
 * 旋转bitmap
 * @param bitmap
 * @param degress 旋转角度
 * @param needRecycle
 * @return
 */
public static Bitmap rotateBitmap(Bitmap bitmap, int degress, boolean needRecycle) {
    Matrix m = new Matrix();
    m.postRotate(degress);
    Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
    if(needRecycle) {
        bitmap.recycle();
    }
    return bm;
}
 
Example 12
Source File: BitmapUtil.java    From ImagePicker with Apache License 2.0 5 votes vote down vote up
/**
 * 将图片按照指定的角度进行旋转
 *
 * @param bitmap 需要旋转的图片
 * @param degree 指定的旋转角度
 * @return 旋转后的图片
 */
public static Bitmap rotateBitmapByDegree(Bitmap bitmap, int degree) {
    // 根据旋转角度,生成旋转矩阵
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    // 将原始图片按照旋转矩阵进行旋转,并得到新的图片
    Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    if (!bitmap.isRecycled()) {
        bitmap.recycle();
    }
    return newBitmap;
}
 
Example 13
Source File: RotateBitmap.java    From fanfouapp-opensource with Apache License 2.0 5 votes vote down vote up
private void invalidate() {
    final Matrix matrix = new Matrix();
    final int cx = this.mBitmapWidth / 2;
    final int cy = this.mBitmapHeight / 2;
    matrix.preTranslate(-cx, -cy);
    matrix.postRotate(this.mRotation);
    matrix.postTranslate(cx, cx);

    final RectF rect = new RectF(0, 0, this.mBitmapWidth,
            this.mBitmapHeight);
    matrix.mapRect(rect);
    this.mWidth = (int) rect.width();
    this.mHeight = (int) rect.height();
}
 
Example 14
Source File: RotateBitmap.java    From memoir with Apache License 2.0 5 votes vote down vote up
public Matrix getRotateMatrix() {
    // By default this is an identity matrix.
    Matrix matrix = new Matrix();
    if (mRotation != 0) {
        // We want to do the rotation at origin, but since the bounding
        // rectangle will be changed after rotation, so the delta values
        // are based on old & new width/height respectively.
        int cx = mBitmap.getWidth() / 2;
        int cy = mBitmap.getHeight() / 2;
        matrix.preTranslate(-cx, -cy);
        matrix.postRotate(mRotation);
        matrix.postTranslate(getWidth() / 2, getHeight() / 2);
    }
    return matrix;
}
 
Example 15
Source File: FlipLoadingLayout.java    From handmarkPulltorefreshLibrary with Apache License 2.0 5 votes vote down vote up
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable) {
	if (null != imageDrawable) {
		final int dHeight = imageDrawable.getIntrinsicHeight();
		final int dWidth = imageDrawable.getIntrinsicWidth();

		/**
		 * We need to set the width/height of the ImageView so that it is
		 * square with each side the size of the largest drawable dimension.
		 * This is so that it doesn't clip when rotated.
		 */
		ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
		lp.width = lp.height = Math.max(dHeight, dWidth);
		mHeaderImage.requestLayout();

		/**
		 * We now rotate the Drawable so that is at the correct rotation,
		 * and is centered.
		 */
		mHeaderImage.setScaleType(ScaleType.MATRIX);
		Matrix matrix = new Matrix();
		matrix.postTranslate((lp.width - dWidth) / 2f,
				(lp.height - dHeight) / 2f);
		matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f,
				lp.height / 2f);
		mHeaderImage.setImageMatrix(matrix);
	}
}
 
Example 16
Source File: FlipLoadingLayoutFooter.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable)
{
    if (null != imageDrawable)
    {
        final int dHeight = imageDrawable.getIntrinsicHeight();
        final int dWidth = imageDrawable.getIntrinsicWidth();

        /**
         * We need to set the width/height of the ImageView so that it is
         * square with each side the size of the largest drawable dimension.
         * This is so that it doesn't clip when rotated.
         */
        ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
        lp.width = lp.height = Math.max(dHeight, dWidth);
        mHeaderImage.requestLayout();

        /**
         * We now rotate the Drawable so that is at the correct rotation,
         * and is centered.
         */
        mHeaderImage.setScaleType(ScaleType.MATRIX);
        Matrix matrix = new Matrix();
        matrix.postTranslate((lp.width - dWidth) / 2f, (lp.height - dHeight) / 2f);
        matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f, lp.height / 2f);
        mHeaderImage.setImageMatrix(matrix);
    }
}
 
Example 17
Source File: RotateBitmap.java    From droidddle with Apache License 2.0 5 votes vote down vote up
public Matrix getRotateMatrix() {
    // By default this is an identity matrix.
    Matrix matrix = new Matrix();
    if (mRotation != 0) {
        // We want to do the rotation at origin, but since the bounding
        // rectangle will be changed after rotation, so the delta values
        // are based on old & new width/height respectively.
        int cx = mBitmap.getWidth() / 2;
        int cy = mBitmap.getHeight() / 2;
        matrix.preTranslate(-cx, -cy);
        matrix.postRotate(mRotation);
        matrix.postTranslate(getWidth() / 2, getHeight() / 2);
    }
    return matrix;
}
 
Example 18
Source File: MainActivity.java    From JavaHTTPUpload with MIT License 5 votes vote down vote up
private void setPic() {
	// Get the dimensions of the View
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor << 1;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    
    Matrix mtx = new Matrix();
    mtx.postRotate(90);
    // Rotating Bitmap
    Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);

    if (rotatedBMP != bitmap)
    	bitmap.recycle();
    
    mImageView.setImageBitmap(rotatedBMP);
    
    try {
		sendPhoto(rotatedBMP);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 19
Source File: CameraActivity.java    From camerademo with Mozilla Public License 2.0 4 votes vote down vote up
public Bitmap rotate(Bitmap bitmap, int degree) {
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
}
 
Example 20
Source File: CropImageView.java    From PictureChooseLib with Apache License 2.0 4 votes vote down vote up
/**
 * Sets a Bitmap and initializes the image rotation according to the EXIT data.
 * <p>
 * The EXIF can be retrieved by doing the following:
 * <code>ExifInterface exif = new ExifInterface(path);</code>
 * 
 * @param bitmap the original bitmap to set; if null, this
 * @param exif the EXIF information about this bitmap; may be null
 */
public void setImageBitmap(Bitmap bitmap, ExifInterface exif) {

    if (bitmap == null) {
        return;
    }

    if (exif == null) {
        setImageBitmap(bitmap);
        return;
    }

    final Matrix matrix = new Matrix();
    final int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    int rotate = -1;

    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
    }

    if (rotate == -1) {
        setImageBitmap(bitmap);
    } else {
        matrix.postRotate(rotate);
        final Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap,
                                                         0,
                                                         0,
                                                         bitmap.getWidth(),
                                                         bitmap.getHeight(),
                                                         matrix,
                                                         true);
        setImageBitmap(rotatedBitmap);
        bitmap.recycle();
    }
}