1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| package com.example.internetpic;
import android.graphics.Bitmap; import android.graphics.Color;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import java.security.MessageDigest;
public class dictionaryTransform extends BitmapTransformation { @Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { return BitmapMosaic(toTransform,40); }
@Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
public static Bitmap BitmapMosaic(Bitmap bitmap, int BLOCK_SIZE) {
if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0 || bitmap.isRecycled()) { return null; } int mBitmapWidth = bitmap.getWidth(); int mBitmapHeight = bitmap.getHeight(); Bitmap mBitmap = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888); int row = mBitmapWidth / BLOCK_SIZE; int col = mBitmapHeight / BLOCK_SIZE; int[] block = new int[BLOCK_SIZE * BLOCK_SIZE]; for (int i = 0; i <=row; i++) { for (int j =0; j <= col; j++) { int length = block.length; int flag = 0; if (i == row && j != col) { length = (mBitmapWidth - i * BLOCK_SIZE) * BLOCK_SIZE; if (length == 0) { break; } bitmap.getPixels(block, 0, BLOCK_SIZE, i * BLOCK_SIZE, j * BLOCK_SIZE, mBitmapWidth - i * BLOCK_SIZE, BLOCK_SIZE);
flag = 1; } else if (i != row && j == col) { length = (mBitmapHeight - j * BLOCK_SIZE) * BLOCK_SIZE; if (length == 0) { break; } bitmap.getPixels(block, 0, BLOCK_SIZE, i * BLOCK_SIZE, j * BLOCK_SIZE, BLOCK_SIZE, mBitmapHeight - j * BLOCK_SIZE); flag = 2; } else if (i == row && j == col) { length = (mBitmapWidth - i * BLOCK_SIZE) * (mBitmapHeight - j * BLOCK_SIZE); if (length == 0) { break; } bitmap.getPixels(block, 0, BLOCK_SIZE, i * BLOCK_SIZE, j * BLOCK_SIZE, mBitmapWidth - i * BLOCK_SIZE, mBitmapHeight - j * BLOCK_SIZE);
flag = 3; } else { bitmap.getPixels(block, 0, BLOCK_SIZE, i * BLOCK_SIZE, j * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); }
int r = 0, g = 0, b = 0, a = 0; for (int k = 0; k < length; k++) { r += Color.red(block[k]); g += Color.green(block[k]); b += Color.blue(block[k]); a += Color.alpha(block[k]); } int color = Color.argb(a / length, r / length, g / length, b / length); for (int k = 0; k < length; k++) { block[k] = color; } if (flag == 1) { mBitmap.setPixels(block, 0, mBitmapWidth - i * BLOCK_SIZE, i * BLOCK_SIZE, j * BLOCK_SIZE, mBitmapWidth - i * BLOCK_SIZE, BLOCK_SIZE); } else if (flag == 2) { mBitmap.setPixels(block, 0, BLOCK_SIZE, i * BLOCK_SIZE, j * BLOCK_SIZE, BLOCK_SIZE, mBitmapHeight - j * BLOCK_SIZE); } else if (flag == 3) { mBitmap.setPixels(block, 0, BLOCK_SIZE, i * BLOCK_SIZE, j * BLOCK_SIZE, mBitmapWidth - i * BLOCK_SIZE, mBitmapHeight - j * BLOCK_SIZE); } else { mBitmap.setPixels(block, 0, BLOCK_SIZE, i * BLOCK_SIZE, j * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); }
} } return mBitmap; } }
|