如何缩放在画布上绘制的位图,同时保存用Matrix指定的旋转和平移 [英] How to scale Bitmap drawn on the Canvas while saving rotation and translation specified with Matrix

查看:139
本文介绍了如何缩放在画布上绘制的位图,同时保存用Matrix指定的旋转和平移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例图像

大家好.我是使用矩阵在android中进行图像处理的新手.我正在开发一个在设备屏幕上显示位图#1的应用程序.

Hi everyone. I am new to Image manipulation in android using matrix. I am working on an app which displays Bitmap #1 on the screen of the device.

位图#1确实很大,约为2592 X 1456,但按比例缩小以适合设备的屏幕尺寸,仅用于显示目的.

Bitmap #1 is really big, about 2592 X 1456, but scaled down to fit the screen size of the device for displaying purposes only.

然后,如上图所示,我使用矩阵(带有旋转,缩放,平移)在Bitmap#1画布上绘制了嘴唇Bitmap(#2).

Then I have drawn the lips Bitmap (#2) on Bitmap #1 Canvas, using matrix (with rotate, scale, translation), as image above is showing.

准确地说,我要保存的是最终位图的副本,该副本将向后缩放为原始大小(2592 x 1456).

Precisely what I want to achieve is to save a copy of the final Bitmap, scaled backwards to the original size (2592 x 1456).

我试图通过缩放Bitmap#1矩阵来实现这一目标.

I tried to achieve it by scaling Bitmap #1 matrix.

这是我到目前为止尝试过的:

This is what I've tried so far:

    // adjust the matrix to the original image size
    // new matrix big
    Matrix newMatrix = new Matrix(); 
    // copy matrix from small matrix
    newMatrix = matrix; 
    RectF src = new RectF(0,0,backgroundImage.getWidth(), backgroundImage.getHeight());
    RectF dst = new RectF(0,0, origBackground.getWidth(), origBackground.getHeight());
    newMatrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
    canvas.drawBitmap(bitmap, newMatrix, paint);

我的问题是,在生成的位图#1中,嘴唇位图(#2)被放置在x = 0和y = 0处,而不是在所需的坐标处,缺少指定的旋转.

My problem is that in the resulting Bitmap #1, lips Bitmap (#2) is being placed at the x=0 and y=0 and not at the required coordinates, missing specified rotation.

推荐答案

一个小时的尝试后.我终于得到了答案.以下是我一直在使用的源代码.感谢@xAF.

After an hour of trying. I finally got the answer. Below is the source code that I've been using. Thanks to @xAF.

干杯

public Bitmap saveBitmap()
   {
    Bitmap bm = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(bm);
    // draw the background photo with its original size
    mCanvas.drawBitmap(bitmap1, 0, 0, null);

    // put the lips and adjust the matrix to the original image size
    Matrix newMatrix = new Matrix(); // new big matrix
    // copy the screen small matrix (with rotate, scale, translation)
    newMatrix.set(matrix);
    float scaleWidth = ((float) bitmap1.getWidth()) / bitmap2.getWidth();
    float scaleHeight = ((float) bitmap1.getHeight()) / bitmap2.getHeight();

    newMatrix.postScale(scaleWidth, scaleHeight, 0, 0);
    mCanvas.drawBitmap(bitmapLips, newMatrix, paint);

    return bm;
}

这篇关于如何缩放在画布上绘制的位图,同时保存用Matrix指定的旋转和平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆