美文网首页
图片拼接

图片拼接

作者: 你吼那么大声干什么 | 来源:发表于2018-12-04 11:34 被阅读0次

拼接两张图片分为两种情况,两者宽度相同和两者宽度不同

两者宽度相同

//创建一个两张图片高度相加的bitmap对象
Bitmap retBmp = Bitmap.createBitmap(width, bitmapFirst.getHeight() + bitmapSecond.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(retBmp);
//依次绘制两张图片至对应bitmap对象
canvas.drawBitmap(bitmapFirst, 0, 0, null);
canvas.drawBitmap(bitmapSecond, 0, bitmapFirst.getHeight(), null);

两者宽度不相同

//获取两张图片的宽度比
int h2 = bitmapSecond.getHeight() * width / bitmapSecond.getWidth();
Bitmap retBmp = Bitmap.createBitmap(width, bitmapFirst.getHeight() + h2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(retBmp);
//以第一张图片的宽度为基准对第二张图片的宽度和高度进行缩放
Bitmap newSizeBmp2 = resizeBitmap(bitmapSecond, width, h2);
//依次绘制两张图片
canvas.drawBitmap(bitmapFirst, 0, 0, null);
canvas.drawBitmap(newSizeBmp2, 0, bitmapFirst.getHeight(), null);

缩放操作

    public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
        float scaleWidth = ((float) newWidth) / bitmap.getWidth();
        float scaleHeight = ((float) newHeight) / bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap bmpScale = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return bmpScale;
    }

附上整个代码:

public class CombineImage {

    //将两张图片合成一张
    public static Bitmap doCombineTask(Bitmap bitmapFirst, Bitmap bitmapSecond){
        Bitmap retBmp;
        int width = bitmapFirst.getWidth();
        if (bitmapSecond.getWidth() != width) {
            //以第一张图片的宽度为标准,对第二张图片进行缩放。
            int h2 = bitmapSecond.getHeight() * width / bitmapSecond.getWidth();
            retBmp = Bitmap.createBitmap(width, bitmapFirst.getHeight() + h2, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(retBmp);
            Bitmap newSizeBmp2 = resizeBitmap(bitmapSecond, width, h2);
            canvas.drawBitmap(bitmapFirst, 0, 0, null);
            canvas.drawBitmap(newSizeBmp2, 0, bitmapFirst.getHeight(), null);
        } else {
            //两张图片宽度相等,则直接拼接。
            retBmp = Bitmap.createBitmap(width, bitmapFirst.getHeight() + bitmapSecond.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(retBmp);
            canvas.drawBitmap(bitmapFirst, 0, 0, null);
            canvas.drawBitmap(bitmapSecond, 0, bitmapFirst.getHeight(), null);
        }

        return retBmp;
    }

    public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
        float scaleWidth = ((float) newWidth) / bitmap.getWidth();
        float scaleHeight = ((float) newHeight) / bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap bmpScale = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return bmpScale;
    }
}

相关文章

  • 图片拼接

  • 图片拼接

    拼接两张图片分为两种情况,两者宽度相同和两者宽度不同 两者宽度相同 两者宽度不相同 缩放操作 附上整个代码:

  • 图片拼接

    图片拼接小工具,帮助你简单快速拼接长图~

  • iOS UIImage图片拼接性能对比

    前言 这篇主要来介绍图片拼接,封装多种拼接方式供使用 多种图片水平和竖直拼接 更多好玩的拼接方式,大致包含平铺、两...

  • canvas图片拼接(横向)

    canvas图片拼接(横向) 之前的工作中遇到的拼接图片问题,于是写了这个图片拼接。大家一起看看吧! 毫无疑问,这...

  • 图片拼接-python

    注:aa.jpg - 第一张图片code.png - 成品图

  • Retrofit2 提交多少图片

    多张不确定图片 主要的是字符拼接部份 单张图片

  • R语言——ggplot2图形拼接

    R语言——ggplot2图形拼接 绘图 图片拼接 method 1 method 2 method 3 —— 图例...

  • 图像处理实战-图像拼接

    图像拼接是将两张具有相同信息的图片,根据相同的信息拼接起来,从而得到全景图片。 完整代码:https://gith...

  • 使用ffmpeg将图片拼接为视频

    本文介绍下如何使用ffmpeg将大量图片拼接成一个视频,并介绍其中部分参数的含义。 使用ffmpeg将图片拼接成视...

网友评论

      本文标题:图片拼接

      本文链接:https://www.haomeiwen.com/subject/nvlmcqtx.html