Search in sources :

Example 76 with Bitmap

use of android.graphics.Bitmap in project Lazy by l123456789jy.

the class BitmapUtil method getBitmapFromView.

/**
     * 从View获取Bitmap
     *
     * @param view View
     * @return Bitmap
     */
public static Bitmap getBitmapFromView(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
    view.draw(canvas);
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas)

Example 77 with Bitmap

use of android.graphics.Bitmap in project Lazy by l123456789jy.

the class BitmapUtil method lumAndHueAndSaturation.

/**
     * 亮度、色相、饱和度处理
     *
     * @param bitmap          原图
     * @param lumValue        亮度值
     * @param hueValue        色相值
     * @param saturationValue 饱和度值
     * @return 亮度、色相、饱和度处理后的图片
     */
public static Bitmap lumAndHueAndSaturation(Bitmap bitmap, int lumValue, int hueValue, int saturationValue) {
    // 计算出符合要求的饱和度值
    float newSaturationValue = saturationValue * 1.0F / 127;
    // 计算出符合要求的亮度值
    float newlumValue = lumValue * 1.0F / 127;
    // 计算出符合要求的色相值
    float newHueValue = (hueValue - 127) * 1.0F / 127 * 180;
    // 创建一个颜色矩阵并设置其饱和度
    ColorMatrix colorMatrix = new ColorMatrix();
    // 设置饱和度值
    colorMatrix.setSaturation(newSaturationValue);
    // 设置亮度值
    colorMatrix.setScale(newlumValue, newlumValue, newlumValue, 1);
    // 控制让红色区在色轮上旋转的角度
    colorMatrix.setRotate(0, newHueValue);
    // 控制让绿红色区在色轮上旋转的角度
    colorMatrix.setRotate(1, newHueValue);
    // 控制让蓝色区在色轮上旋转的角度
    colorMatrix.setRotate(2, newHueValue);
    // 创建一个画笔并设置其颜色过滤器
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
    // 创建一个新的图片并创建画布
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    // 将原图使用给定的画笔画到画布上
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return newBitmap;
}
Also used : ColorMatrixColorFilter(android.graphics.ColorMatrixColorFilter) Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) ColorMatrix(android.graphics.ColorMatrix) Paint(android.graphics.Paint)

Example 78 with Bitmap

use of android.graphics.Bitmap in project Lazy by l123456789jy.

the class BitmapUtil method getBitmapFromView2.

/**
     * 把一个View的对象转换成bitmap
     *
     * @param view View
     * @return Bitmap
     */
public static Bitmap getBitmapFromView2(View view) {
    view.clearFocus();
    view.setPressed(false);
    // 能画缓存就返回false
    boolean willNotCache = view.willNotCacheDrawing();
    view.setWillNotCacheDrawing(false);
    int color = view.getDrawingCacheBackgroundColor();
    view.setDrawingCacheBackgroundColor(0);
    if (color != 0) {
        view.destroyDrawingCache();
    }
    view.buildDrawingCache();
    Bitmap cacheBitmap = view.getDrawingCache();
    if (cacheBitmap == null) {
        if (DEBUG) {
            Log.e(TAG, "failed getViewBitmap(" + view + ")", new RuntimeException());
        }
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
    // Restore the view
    view.destroyDrawingCache();
    view.setWillNotCacheDrawing(willNotCache);
    view.setDrawingCacheBackgroundColor(color);
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint)

Example 79 with Bitmap

use of android.graphics.Bitmap in project Lazy by l123456789jy.

the class BitmapUtil method combineImagesToSameSize.

/**
     * 合并
     * @param bgd 后景Bitmap
     * @param fg 前景Bitmap
     * @return 合成后Bitmap
     */
public static Bitmap combineImagesToSameSize(Bitmap bgd, Bitmap fg) {
    Bitmap bmp;
    int width = bgd.getWidth() < fg.getWidth() ? bgd.getWidth() : fg.getWidth();
    int height = bgd.getHeight() < fg.getHeight() ? bgd.getHeight() : fg.getHeight();
    if (fg.getWidth() != width && fg.getHeight() != height) {
        fg = zoom(fg, width, height);
    }
    if (bgd.getWidth() != width && bgd.getHeight() != height) {
        bgd = zoom(bgd, width, height);
    }
    bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
    Canvas canvas = new Canvas(bmp);
    canvas.drawBitmap(bgd, 0, 0, null);
    canvas.drawBitmap(fg, 0, 0, paint);
    return bmp;
}
Also used : Bitmap(android.graphics.Bitmap) PorterDuffXfermode(android.graphics.PorterDuffXfermode) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint)

Example 80 with Bitmap

use of android.graphics.Bitmap in project Lazy by l123456789jy.

the class BitmapUtil method sunshine.

/**
     * 光照效果处理
     *
     * @param bitmap  原图
     * @param centerX 光源在X轴的位置
     * @param centerY 光源在Y轴的位置
     * @return 光照效果处理后的图片
     */
public static Bitmap sunshine(Bitmap bitmap, int centerX, int centerY) {
    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
    int pixR = 0;
    int pixG = 0;
    int pixB = 0;
    int pixColor = 0;
    int newR = 0;
    int newG = 0;
    int newB = 0;
    int radius = Math.min(centerX, centerY);
    // 光照强度 100~150
    final float strength = 150F;
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    int pos = 0;
    for (int i = 1, length = height - 1; i < length; i++) {
        for (int k = 1, len = width - 1; k < len; k++) {
            pos = i * width + k;
            pixColor = pixels[pos];
            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);
            newR = pixR;
            newG = pixG;
            newB = pixB;
            // 计算当前点到光照中心的距离,平面座标系中求两点之间的距离
            int distance = (int) (Math.pow((centerY - i), 2) + Math.pow(centerX - k, 2));
            if (distance < radius * radius) {
                // 按照距离大小计算增加的光照值
                int result = (int) (strength * (1.0 - Math.sqrt(distance) / radius));
                newR = pixR + result;
                newG = pixG + result;
                newB = pixB + result;
            }
            newR = Math.min(255, Math.max(0, newR));
            newG = Math.min(255, Math.max(0, newG));
            newB = Math.min(255, Math.max(0, newB));
            pixels[pos] = Color.argb(255, newR, newG, newB);
        }
    }
    newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBitmap;
}
Also used : Bitmap(android.graphics.Bitmap) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint)

Aggregations

Bitmap (android.graphics.Bitmap)3746 Canvas (android.graphics.Canvas)889 Paint (android.graphics.Paint)709 BitmapDrawable (android.graphics.drawable.BitmapDrawable)461 IOException (java.io.IOException)394 Rect (android.graphics.Rect)341 File (java.io.File)262 Test (org.junit.Test)255 Matrix (android.graphics.Matrix)254 Drawable (android.graphics.drawable.Drawable)243 BitmapFactory (android.graphics.BitmapFactory)240 View (android.view.View)225 ImageView (android.widget.ImageView)210 Intent (android.content.Intent)187 FileOutputStream (java.io.FileOutputStream)186 InputStream (java.io.InputStream)171 RectF (android.graphics.RectF)150 FileNotFoundException (java.io.FileNotFoundException)150 Point (android.graphics.Point)148 Uri (android.net.Uri)128