以下是一个从文件中读取两张宽度相同的图片然后进行纵向拼接的方法的示例代码:

public Bitmap mergeBitmaps(String filePath1, String filePath2) {
    Bitmap bitmap1 = BitmapFactory.decodeFile(filePath1);
    Bitmap bitmap2 = BitmapFactory.decodeFile(filePath2);

    if (bitmap1.getWidth() != bitmap2.getWidth()) {
        return null;
    }

    int width = bitmap1.getWidth();
    int height = bitmap1.getHeight() + bitmap2.getHeight();

    Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(resultBitmap);

    canvas.drawBitmap(bitmap1, 0, 0, null);
    canvas.drawBitmap(bitmap2, 0, bitmap1.getHeight(), null);

    return resultBitmap;
}

此方法接收两个参数,分别是两张图片的文件路径。首先,它使用BitmapFactory类从文件中读取两张图片并将它们分别存储在bitmap1bitmap2变量中。在检查这两张图片的宽度是否相同后,它创建一个新的Bitmap对象resultBitmap,该对象的宽度与输入图像的宽度相同,而高度是两张图像的高度之和。接着,它创建一个Canvas对象,并在其中绘制这两张图像,第一张图像位于(0,0),第二张图像位于(0,bitmap1.getHeight()),也就是第一张图像的下方。最后,它返回拼接后的Bitmap对象

在andriod中写一个从文件中读取两张宽度相同的图片然后进行纵向拼接的方法

原文地址: https://www.cveoy.top/t/topic/fs2I 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录