【问题标题】:Scaling matrix transformed images for Compositing缩放矩阵变换图像以进行合成
【发布时间】:2011-08-03 23:26:56
【问题描述】:

我正在设置一个相机视图,它的顶部有一个图像视图,并使用矩阵变换在屏幕上移动图像。当我拍摄照片时,我希望能够以正确的位置和缩放比例将图像合成到照片上。照片只能是纵向的,如果是横向的,则会旋转。

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)

矩形:(359.8342,901.34326) w:654.6885 h:772.1968

图像 rect 的缩放比例似乎不够大,因为生成的合成图像中的字符离上/左太远且太小。

【问题讨论】:

    标签: android matrix bitmap scale composite


    【解决方案1】:

    问题是 android 对字符图像进行了预缩放,这导致了我所有的缩放计算

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

    【讨论】:

    • 另一种选择是使用getScaledWidth() 考虑屏幕dpi
    猜你喜欢
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 2021-12-29
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    相关资源
    最近更新 更多