缩放矩阵变换图像以进行合成

时间:2011-08-03 23:26:56

标签: android matrix bitmap scale composite

我正在设置一个摄像机视图,其顶部有一个图像视图,并使用矩阵变换在屏幕上移动图像。当我拍摄照片时,我希望能够将图像合成到照片上,并正确地考虑位置和缩放。这张照片只是一幅肖像,如果是风景照片就会旋转。

public void onPictureTaken(byte[] data, Camera camera) {

    BitmapFactory.Options opt = new BitmapFactory.Options();
    this.newPhoto = BitmapFactory.decodeByteArray(data, 0, data.length, opt);

    int photoWidth = newPhoto.getWidth() > newPhoto.getHeight()?newPhoto.getHeight():newPhoto.getWidth();
    int photoHeight = newPhoto.getWidth() > newPhoto.getHeight()?newPhoto.getWidth():newPhoto.getHeight();

    //Preview is fullscreen
    Display display = getWindowManager().getDefaultDisplay(); 
    int previewWidth = display.getWidth();
    int previewHeight = display.getHeight();


    float scale = (float)photoHeight / (float)previewHeight;
    float scaleX = (float)photoWidth / (float)previewWidth;

    Bitmap finished = Bitmap.createBitmap(photoWidth, photoHeight, newPhoto.getConfig());

    //Create canvas and draw photo into it 
    //[snip]

    //Draw character onto canvas
    int imageResource = getResources().getIdentifier(character + "large", "drawable", getPackageName());
    if (imageResource != 0) {
        Bitmap character = BitmapFactory.decodeResource(this.getResources(), imageResource);
        RectF rect = new RectF();
        float[] values = new float[9];

        matrix.getValues(values);
        rect.left = values[Matrix.MTRANS_X] * scaleX;
        rect.top = values[Matrix.MTRANS_Y] * scale;
        //273x322 WidthxHeight of preview character image
        rect.right = rect.left + (273 * values[Matrix.MSCALE_X]) * scale;
        rect.bottom = rect.top + (322 * values[Matrix.MSCALE_Y]) * scale;

        //Logging here


        canvas.drawBitmap(character, null, rect, null);
        character.recycle();
    }


    System.gc();
    newPhoto = finished;
    showDialog(DIALOG_PHOTO_RESULT);
}

日志:

  

预览:480x854照片:1536x2048

     

规模:2.3981264

     

矩阵:(112.45,375.85)比例:(1.00,1.00)

     

Rect:(359.8342,901.34326)w:654.6885 h:772.1968

图像rect似乎没有足够大的缩放比例,因为生成的合成图像的字符朝向顶部/左侧太远而且太小。

1 个答案:

答案 0 :(得分:1)

问题是android正在预先缩放字符图像,这会丢弃我的所有比例计算

通过将角色图像移动到drawable-nodpi文件夹,我可以停止android缩放设备的图像,以便可以缩放图像以匹配摄像机视图。