XBitmapUtils.java 文件源码

java
阅读 39 收藏 0 点赞 0 评论 0

项目:XFrame 作者:
/**
 * 将彩色图转换为黑白图
 *
 * @param bmp 位图
 * @return 返回转换好的位图
 */
public static Bitmap convertToBlackWhite(Bitmap bmp) {
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);

    int alpha = 0xFF << 24; // 默认将bitmap当成24色图片
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int grey = pixels[width * i + j];

            int red = ((grey & 0x00FF0000) >> 16);
            int green = ((grey & 0x0000FF00) >> 8);
            int blue = (grey & 0x000000FF);

            grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
            grey = alpha | (grey << 16) | (grey << 8) | grey;
            pixels[width * i + j] = grey;
        }
    }
    Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
    newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBmp;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号