圖片合成工具類(BitmapMergeUtils)

小小9575發表於2018-10-16
package com.reader.xdkk.ydq.app.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;

/**
 * 作者:  GJP on 2018/8/22 09:27
 * 郵箱:  xiaoxiao9575@126.com
 * 部落格:  https://blog.csdn.net/weixin_40998254
 * 描述:  圖片合成工具類
 */

public class BitmapMergeUtils {
    /**
     * 把兩個點陣圖覆蓋合成為一個點陣圖,以底層點陣圖的長寬為基準
     * @param backBitmap 在底部的點陣圖
     * @param backWidth 底部點陣圖得寬度
     * @param backHeight 底部點陣圖得高度
     * @param frontBitmap 蓋在上面的點陣圖
     * @param frontLeft 頂部點陣圖橫向起點
     * @param frontTop 頂部點陣圖縱向起點
     * @param frontWidth 頂部點陣圖寬度
     * @param frontHeight 頂部點陣圖高度
     * @return
     */
    public static Bitmap mergeBitmap(Bitmap backBitmap, int backWidth, int backHeight, Bitmap frontBitmap, int frontLeft, int frontTop, int frontWidth, int frontHeight) {

        if (backBitmap == null || backBitmap.isRecycled()
                || frontBitmap == null || frontBitmap.isRecycled()) {
            return null;
        }
        // 調整背景圖片到指定得寬高
        int width = backBitmap.getWidth();
        int height = backBitmap.getHeight();
        if (width != backWidth || height != backHeight){
            // 計算縮放比例.
            float scaleWidth = ((float) backWidth) / width;
            float scaleHeight = ((float) backHeight) / height;
            // 取得想要縮放的matrix引數.
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的圖片.
            backBitmap = Bitmap.createBitmap(backBitmap, 0, 0, width, height, matrix, true);
        }
        Bitmap bitmap = backBitmap.copy(Config.RGB_565, true);
        //繪製前景圖片到指定位置
        Canvas canvas = new Canvas(bitmap);
        Rect frontRect  = new Rect(0, 0, frontBitmap.getWidth(), frontBitmap.getHeight());
        Rect drawRect = new Rect(frontLeft, frontTop, frontLeft+frontWidth, frontTop+frontHeight);
        canvas.drawBitmap(frontBitmap, frontRect, drawRect, null);
        return bitmap;
    }

    /**
     * 把兩個點陣圖覆蓋合成為一個點陣圖,左右拼接
     * @param leftBitmap
     * @param rightBitmap
     * @param isBaseMax 是否以寬度大的點陣圖為準,true則小圖等比拉伸,false則大圖等比壓縮
     * @return
     */
    public static Bitmap mergeBitmap_LR(Bitmap leftBitmap, Bitmap rightBitmap, boolean isBaseMax) {

        if (leftBitmap == null || leftBitmap.isRecycled()
                || rightBitmap == null || rightBitmap.isRecycled()) {
            return null;
        }
        int height = 0; // 拼接後的高度,按照引數取大或取小
        if (isBaseMax) {
            height = leftBitmap.getHeight() > rightBitmap.getHeight() ? leftBitmap.getHeight() : rightBitmap.getHeight();
        } else {
            height = leftBitmap.getHeight() < rightBitmap.getHeight() ? leftBitmap.getHeight() : rightBitmap.getHeight();
        }

        // 縮放之後的bitmap
        Bitmap tempBitmapL = leftBitmap;
        Bitmap tempBitmapR = rightBitmap;

        if (leftBitmap.getHeight() != height) {
            tempBitmapL = Bitmap.createScaledBitmap(leftBitmap, (int)(leftBitmap.getWidth()*1f/leftBitmap.getHeight()*height), height, false);
        } else if (rightBitmap.getHeight() != height) {
            tempBitmapR = Bitmap.createScaledBitmap(rightBitmap, (int)(rightBitmap.getWidth()*1f/rightBitmap.getHeight()*height), height, false);
        }

        // 拼接後的寬度
        int width = tempBitmapL.getWidth() + tempBitmapR.getWidth();

        // 定義輸出的bitmap
        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);

        // 縮放後兩個bitmap需要繪製的引數
        Rect leftRect = new Rect(0, 0, tempBitmapL.getWidth(), tempBitmapL.getHeight());
        Rect rightRect  = new Rect(0, 0, tempBitmapR.getWidth(), tempBitmapR.getHeight());

        // 右邊圖需要繪製的位置,往右邊偏移左邊圖的寬度,高度是相同的
        Rect rightRectT  = new Rect(tempBitmapL.getWidth(), 0, width, height);

        canvas.drawBitmap(tempBitmapL, leftRect, leftRect, null);
        canvas.drawBitmap(tempBitmapR, rightRect, rightRectT, null);
        return bitmap;
    }


    /**
     * 把兩個點陣圖覆蓋合成為一個點陣圖,上下拼接
     * @param topBitmap
     * @param bottomBitmap
     * @param isBaseMax 是否以高度大的點陣圖為準,true則小圖等比拉伸,false則大圖等比壓縮
     * @return
     */
    public static Bitmap mergeBitmap_TB(Bitmap topBitmap, Bitmap bottomBitmap, boolean isBaseMax) {

        if (topBitmap == null || topBitmap.isRecycled()
                || bottomBitmap == null || bottomBitmap.isRecycled()) {
            return null;
        }
        int width = 0;
        if (isBaseMax) {
            width = topBitmap.getWidth() > bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();
        } else {
            width = topBitmap.getWidth() < bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();
        }
        Bitmap tempBitmapT = topBitmap;
        Bitmap tempBitmapB = bottomBitmap;

        if (topBitmap.getWidth() != width) {
            tempBitmapT = Bitmap.createScaledBitmap(topBitmap, width, (int)(topBitmap.getHeight()*1f/topBitmap.getWidth()*width), false);
        } else if (bottomBitmap.getWidth() != width) {
            tempBitmapB = Bitmap.createScaledBitmap(bottomBitmap, width, (int)(bottomBitmap.getHeight()*1f/bottomBitmap.getWidth()*width), false);
        }

        int height = tempBitmapT.getHeight() + tempBitmapB.getHeight();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);

        Rect topRect = new Rect(0, 0, tempBitmapT.getWidth(), tempBitmapT.getHeight());
        Rect bottomRect  = new Rect(0, 0, tempBitmapB.getWidth(), tempBitmapB.getHeight());

        Rect bottomRectT  = new Rect(0, tempBitmapT.getHeight(), width, height);

        canvas.drawBitmap(tempBitmapT, topRect, topRect, null);
        canvas.drawBitmap(tempBitmapB, bottomRect, bottomRectT, null);
        return bitmap;
    }


}

 

相關文章