kotlin 如何创建一个(1280 x 720)位图在android?

yyhrrdl8  于 2022-12-13  发布在  Kotlin
关注(0)|答案(1)|浏览(106)

我尝试合并两个640像素宽的位图,但结果总是一个1024宽的位图。我如何让android创建一个1280宽的位图?
我使用下面的代码来缩放我的位图:

public static Bitmap scaleWidthImage(Bitmap image, int destWidth) {
    int origWidth = image.getWidth();
    int origHeight = image.getHeight();
    int destHeight;
    if (origWidth > destWidth) {
        destHeight = origHeight / (origWidth / destWidth);
    } else {
        destHeight = origHeight * (destWidth / origWidth);
    }
    image = Bitmap.createScaledBitmap(image, destWidth, destHeight, false);
    return image;
}

这段代码用于将两个位图合并为一个

fun mergeBitmap(fr: Bitmap, sc: Bitmap): Bitmap? {
    val comboBitmap: Bitmap
    val width: Int = fr.width + sc.width
    val height: Int = fr.height
    comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    val comboImage = Canvas(comboBitmap)
    comboImage.drawBitmap(fr, 0f, 0f, null)
    comboImage.drawBitmap(sc, fr.width.toFloat(), 0f, null)
    return comboBitmap
}

当两个位图的宽度都小于1024像素时,合并后的位图具有所需的宽度,但当两者之和大于1024像素时,合并后的位图就不具有所需的宽度。

cbjzeqam

cbjzeqam1#

我试过下面两张图片:

640x640图像:

640x800图像:

我创建了这个方法来将两个位图合并为一个:

public static Bitmap drawBitmapsHorizontally(Bitmap bitmapOne, Bitmap bitmapTwo) {
        ArrayList<Bitmap> bitmaps = new ArrayList<>();
        bitmaps.add(bitmapOne);
        bitmaps.add(bitmapTwo);
        int width = 0;
        for (Bitmap map : bitmaps)
            width += map.getWidth();

// you can set your favorite height (e.g. 720) instead of getting the max height of the two bitmaps.

        Bitmap bitmap = Bitmap.createBitmap(width,
                Math.max(bitmapOne.getHeight(),bitmapTwo.getHeight()),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.setDensity(DisplayMetrics.DENSITY_MEDIUM);
        bitmap.setDensity(DisplayMetrics.DENSITY_MEDIUM);
        canvas.drawBitmap(bitmapOne, 0f, 0f, null);
        canvas.drawBitmap(bitmapTwo, bitmapOne.getWidth(), 0f, null);
        return bitmap;
    }

结果1280x800

相关问题