Android 中图能够用到的图片处理类 BitmapUtils

Android在实际开发中非常多时候都要对图片进行一定的处理,这里总结的BitmapUtils 类包含一下几个功能:

1.Android图片倒影,

2.Android图片模糊处理,

3.Android图片圆角处理,

4.图片沿着y轴旋转一定角度,

5.Android给图片加入边框。

6.将View 转换成bitmap

7.将大图片二次压缩

8.压缩图片获得缩略图

9.获得本地文件视频的缩略图

10.获得本地文件图片的缩略图

接下来就直接上代码了,代码中有一定的解释。

直接哪来用就能够了。

</pre><pre name="code" class="java">/*
 * @Title: BitmapUtils.java
 * @Description: TODO<请描写叙述此文件是做什么的>
 * @author: xjp
 * @data: 2014年9月18日 上午10:10:30
 * @version: V1.0
 */
package com.mktech.bitmaputils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;

/**
 * TODO<请描写叙述这个类是干什么的>
 *
 * @author xjp
 * @data: 2014年9月18日 上午10:10:30
 * @version: V1.0
 */
public class BitmapUtils {

    private static final String TAG = "BitmapUtils";

    /**
     * TODO<将控件转换成bitmap类型>
     *
     * @throw
     * @return Bitmap
     * @param paramView
     *            :须要转换的控件
     */
    public static Bitmap convertViewToBitmap(View paramView) {
        paramView.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        paramView.layout(0, 0, paramView.getMeasuredWidth(),
                paramView.getMeasuredHeight());
        paramView.buildDrawingCache();
        return paramView.getDrawingCache();
    }

    /**
     * TODO<创建倒影图片>
     *
     * @throw
     * @return Bitmap
     * @param srcBitmap
     *            源图片的bitmap
     * @param reflectionHeight
     *            图片倒影的高度
     */
    public static Bitmap createReflectedBitmap(Bitmap srcBitmap,
            int reflectionHeight) {

        if (null == srcBitmap) {
            Log.e(TAG, "the srcBitmap is null");
            return null;
        }

        // The gap between the reflection bitmap and original bitmap.
        final int REFLECTION_GAP = 0;

        int srcWidth = srcBitmap.getWidth();
        int srcHeight = srcBitmap.getHeight();

        if (0 == srcWidth || srcHeight == 0) {
            Log.e(TAG, "the srcBitmap is null");
            return null;
        }

        // The matrix
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        try {

            // The reflection bitmap, width is same with original‘s, height is
            // half of original‘s.
            Bitmap reflectionBitmap = Bitmap.createBitmap(srcBitmap, 0,
                    srcHeight - reflectionHeight, srcWidth, reflectionHeight,
                    matrix, false);

            if (null == reflectionBitmap) {
                Log.e(TAG, "Create the reflectionBitmap is failed");
                return null;
            }

            // Create the bitmap which contains original and reflection bitmap.
            Bitmap bitmapWithReflection = Bitmap.createBitmap(srcWidth,
                    srcHeight + reflectionHeight, Bitmap.Config.ARGB_8888);

            if (null == bitmapWithReflection) {
                return null;
            }

            // Prepare the canvas to draw stuff.
            Canvas canvas = new Canvas(bitmapWithReflection);

            // Draw the original bitmap.
            canvas.drawBitmap(srcBitmap, 0, 0, null);

            // Draw the reflection bitmap.
            canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP,
                    null);

            Paint paint = new Paint();
            paint.setAntiAlias(true);
            LinearGradient shader = new LinearGradient(0, srcHeight, 0,
                    bitmapWithReflection.getHeight() + REFLECTION_GAP,
                    0x70FFFFFF, 0x00FFFFFF, TileMode.MIRROR);
            paint.setShader(shader);
            paint.setXfermode(new PorterDuffXfermode(
                    android.graphics.PorterDuff.Mode.DST_IN));

            canvas.save();
            // Draw the linear shader.
            canvas.drawRect(0, srcHeight, srcWidth,
                    bitmapWithReflection.getHeight() + REFLECTION_GAP, paint);
            if (reflectionBitmap != null && !reflectionBitmap.isRecycled()) {
                reflectionBitmap.recycle();
                reflectionBitmap = null;
            }

            canvas.restore();

            return bitmapWithReflection;
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e(TAG, "Create the reflectionBitmap is failed");
        return null;
    }

    /**
     * TODO<图片圆角处理>
     *
     * @throw
     * @return Bitmap
     * @param srcBitmap
     *            源图片的bitmap
     * @param ret
     *            圆角的度数
     */
    public static Bitmap getRoundImage(Bitmap srcBitmap, float ret) {

        if (null == srcBitmap) {
            Log.e(TAG, "the srcBitmap is null");
            return null;
        }

        int bitWidth = srcBitmap.getWidth();
        int bitHight = srcBitmap.getHeight();

        BitmapShader bitmapShader = new BitmapShader(srcBitmap,
                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(bitmapShader);

        RectF rectf = new RectF(0, 0, bitWidth, bitHight);

        Bitmap outBitmap = Bitmap.createBitmap(bitWidth, bitHight,
                Config.ARGB_8888);
        Canvas canvas = new Canvas(outBitmap);
        canvas.drawRoundRect(rectf, ret, ret, paint);
        canvas.save();
        canvas.restore();

        return outBitmap;
    }

    /**
     * TODO<图片沿着Y轴旋转一定角度>
     *
     * @throw
     * @return Bitmap
     * @param srcBitmap
     *            源图片的bitmap
     * @param reflectionHeight
     *            图片倒影的高度
     * @param rotate
     *            图片旋转的角度
     */
    public static Bitmap skewImage(Bitmap srcBitmap, float rotate,
            int reflectionHeight) {

        if (null == srcBitmap) {
            Log.e(TAG, "the srcBitmap is null");
            return null;
        }

        Bitmap reflecteBitmap = createReflectedBitmap(srcBitmap,
                reflectionHeight);

        if (null == reflecteBitmap) {
            Log.e(TAG, "failed to createReflectedBitmap");
            return null;
        }

        int wBitmap = reflecteBitmap.getWidth();
        int hBitmap = reflecteBitmap.getHeight();
        float scaleWidth = ((float) 180) / wBitmap;
        float scaleHeight = ((float) 270) / hBitmap;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        reflecteBitmap = Bitmap.createBitmap(reflecteBitmap, 0, 0, wBitmap,
                hBitmap, matrix, true);
        Camera localCamera = new Camera();
        localCamera.save();
        Matrix localMatrix = new Matrix();
        localCamera.rotateY(rotate);
        localCamera.getMatrix(localMatrix);
        localCamera.restore();
        localMatrix.preTranslate(-reflecteBitmap.getWidth() >> 1,
                -reflecteBitmap.getHeight() >> 1);
        Bitmap localBitmap2 = Bitmap.createBitmap(reflecteBitmap, 0, 0,
                reflecteBitmap.getWidth(), reflecteBitmap.getHeight(),
                localMatrix, true);
        Bitmap localBitmap3 = Bitmap.createBitmap(localBitmap2.getWidth(),
                localBitmap2.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas localCanvas = new Canvas(localBitmap3);
        Paint localPaint = new Paint();
        localPaint.setAntiAlias(true);
        localPaint.setFilterBitmap(true);
        localCanvas.drawBitmap(localBitmap2, 0.0F, 0.0F, localPaint);
        if (null != reflecteBitmap && !reflecteBitmap.isRecycled()) {
            reflecteBitmap.recycle();
            reflecteBitmap = null;
        }
        if (null != localBitmap2 && !localBitmap2.isRecycled()) {
            localBitmap2.recycle();
            localBitmap2 = null;
        }
        localCanvas.save();
        localCanvas.restore();
        return localBitmap3;
    }

    /**
     * TODO<图片模糊化处理>
     *
     * @throw
     * @return Bitmap
     * @param bitmap
     *            源图片
     * @param radius
     *            The radius of the blur Supported range 0 < radius <= 25
     * @param context
     *            上下文
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @SuppressLint("NewApi")
    public static Bitmap blurBitmap(Bitmap bitmap, float radius, Context context) {

        // Let‘s create an empty bitmap with the same size of the bitmap we want
        // to blur
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);

        // Instantiate a new Renderscript
        RenderScript rs = RenderScript.create(context);

        // Create an Intrinsic Blur Script using the Renderscript
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs,
                Element.U8_4(rs));

        // Create the Allocations (in/out) with the Renderscript and the in/out
        // bitmaps
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

        // Set the radius of the blur
        if (radius > 25) {
            radius = 25.0f;
        } else if (radius <= 0) {
            radius = 1.0f;
        }
        blurScript.setRadius(radius);

        // Perform the Renderscript
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);

        // Copy the final bitmap created by the out Allocation to the outBitmap
        allOut.copyTo(outBitmap);

        // recycle the original bitmap
        bitmap.recycle();
        bitmap = null;
        // After finishing everything, we destroy the Renderscript.
        rs.destroy();

        return outBitmap;

    }

    /**
     * TODO<给图片加入指定颜色的边框>
     *
     * @param srcBitmap
     *            原图片
     * @param borderWidth
     *            边框宽度
     * @param color
     *            边框的颜色值
     * @return
     */
    public static Bitmap addFrameBitmap(Bitmap srcBitmap, int borderWidth,
            int color) {
        if (srcBitmap == null) {
            Log.e(TAG, "the srcBitmap or borderBitmap is null");
            return null;
        }

        int newWidth = srcBitmap.getWidth() + borderWidth;
        int newHeight = srcBitmap.getHeight() + borderWidth;

        Bitmap outBitmap = Bitmap.createBitmap(newWidth, newHeight,
                Config.ARGB_8888);

        Canvas canvas = new Canvas(outBitmap);

        Rect rec = canvas.getClipBounds();
        rec.bottom--;
        rec.right--;
        Paint paint = new Paint();
        // 设置边框颜色
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
        // 设置边框宽度
        paint.setStrokeWidth(borderWidth);
        canvas.drawRect(rec, paint);

        canvas.drawBitmap(srcBitmap, borderWidth / 2, borderWidth / 2, null);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        if (srcBitmap != null && !srcBitmap.isRecycled()) {
            srcBitmap.recycle();
            srcBitmap = null;
        }

        return outBitmap;
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res,
            int resId, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        // 先将inJustDecodeBounds属性设置为true,解码避免内存分配
        options.inJustDecodeBounds = true;
        // 将图片传入选择器中
        BitmapFactory.decodeResource(res, resId, options);
        // 对图片进行指定比例的压缩
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
        // 待图片处理完毕后再进行内存的分配,避免内存泄露的发生
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    // 计算图片的压缩比例
    public static int calculateInSampleSize(BitmapFactory.Options option,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = option.outHeight;
        final int width = option.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            // 选择长宽高较小的比例。成为压缩比例
            inSampleSize = heightRatio < widthRatio ?

heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    /**
     * Creates a centered bitmap of the desired size.
     *
     * @param source
     *            original bitmap source
     * @param width
     *            targeted width
     * @param height
     *            targeted height
     */
    public static Bitmap Thumbnail(Bitmap bm, int reqWidth, int reqHeight) {
        Bitmap bmp = null;
        bmp = ThumbnailUtils.extractThumbnail(bm, reqWidth, reqHeight);
        return bmp;
    }

    /**
     * Creates a centered bitmap of the desired size.
     *
     * @param source
     *            original bitmap source
     * @param width
     *            targeted width
     * @param height
     *            targeted height
     * @param options
     *            options used during thumbnail extraction
     */
    public static Bitmap Thumbnail(Bitmap bm, int reqWidth, int reqHeight,
            int options) {
        Bitmap bmp = null;
        bmp = ThumbnailUtils.extractThumbnail(bm, reqWidth, reqHeight, options);
        return bmp;
    }

    //创建文件视频的缩略图
    /**
     * Create a video thumbnail for a video. May return null if the video is
     * corrupt or the format is not supported.
     *
     * @param filePath
     *            the path of video file
     * @param kind
     *            could be MINI_KIND or MICRO_KIND
     */
    public static Bitmap createVideoThumbnail(String filePath, int kind) {
        return ThumbnailUtils.createVideoThumbnail(filePath, kind);
    }

    //创建文件图片的缩略图
    /**
     * This method first examines if the thumbnail embedded in EXIF is bigger
     * than our target size. If not, then it‘ll create a thumbnail from original
     * image. Due to efficiency consideration, we want to let MediaThumbRequest
     * avoid calling this method twice for both kinds, so it only requests for
     * MICRO_KIND and set saveImage to true. This method always returns a
     * "square thumbnail" for MICRO_KIND thumbnail.
     *
     * @param filePath
     *            the path of image file
     * @param kind
     *            could be Images.Thumbnails.MINI_KIND or
     *            Images.Thumbnails.MINI_KIND
     * @return Bitmap, or null on failures
     * @hide This method is only used by media framework and media provider
     *       internally.
     */
    public static Bitmap createImageThumbnail(String filePath, int kind) {
        return ThumbnailUtils.createImageThumbnail(filePath, kind);
    }
}

以上代码经验证能够直接使用。

分享。

完结。

时间: 2024-12-07 14:54:11

Android 中图能够用到的图片处理类 BitmapUtils的相关文章

Android 中图可以用到的图片处理类 BitmapUtils

Android在实际开发中很多时候都要对图片进行一定的处理,这里总结的BitmapUtils 类包括一下几个功能: 1.Android图片倒影, 2.Android图片模糊处理, 3.Android图片圆角处理, 4.图片沿着y轴旋转一定角度, 5.Android给图片添加边框. 接下来就直接上代码了,代码中有一定的解释.直接哪来用就可以了. /* * @Title: BitmapUtils.java * @Copyright: Corporation. Ltd. Copyright 1998-

android中图型的阴影效果(shadow-effect-with-custom-shapes)

思路: 在自定义shape中增加一层或多层,并错开,即可显示阴影效果.为增加立体感,按钮按下的时候,只设置一层.我们可以通过top, bottom, right 和 left 四个参数来控制阴影的方向和大小. 关系图 以下自定义两种阴影效果: res/drawable-hdpi/shadow1.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http:

(转)Android中实现区域平均算法在图片缩放里的应用(缩放图片抗锯齿)

摘要:Android图片缩放效果较差,尤其是将大尺寸的图片缩放成小尺寸的图片时,即便是加了抗锯齿,锯齿现象也比较严重:而java sdk里的区域平均算法缩放图片,效果就比较完美了,因为jdk不能直接用于安卓项目中(类冲突),也没找到可以使用的替代的library,最终只好自己写,在此分享! 正文: 目前我知道的Android API中的传统的图片抗锯齿优化处理无非就是以下相关的设置: //缩放抗锯齿Bitmap.createScaledBitmap(bitmap, width, height,

Android中直播视频技术探究之---基础核心类ByteBuffer解析

一.前言 前一篇文章我们介绍了Android中直播视频技术的基础大纲知识,这里就开始一一讲解各个知识点,首先主要来看一下视频直播中的一个重要的基础核心类:ByteBuffer,这个类看上去都知道了,是字节缓冲区处理字节的,这个类的功能非常强大,也在各个场景都有用到,比如网络数据底层处理,特别是结合网络通道信息处理的时候,还有就是后面要说到的OpenGL技术也要用到,当然在视频处理中也是很重要的,因为要处理视频流信息,比如在使用MediaCodec进行底层的视频流编码的时候,处理的就是字节,我们如

关于android中调用系统拍照,返回图片是旋转90度..

由于项目的需要,没有自定义拍照功能,仅仅调用了系统的拍照程序..但是出现了一个问题,就是拍照完成显示图片居然是被旋转的图片.... 解决办法: /** * 获取图片的旋转角度,有些系统把拍照的图片旋转了,有的没有旋转 */ int degree = readPictureDegree(f.getAbsolutePath()); BitmapFactory.Options opts=new BitmapFactory.Options();//获取缩略图显示到屏幕上 opts.inSampleSiz

Android中Bitmap,byte[],Drawable,InputStream相互转化工具类

在新浪看到一些数据流的转化,分享一下: 1.将byte[]转换成InputStream   public InputStream Byte2InputStream(byte[] b) { ByteArrayInputStream bais = new ByteArrayInputStream(b); return bais; } 2. 将InputStream转换成byte[]   两种方式: 一. public static final byte[] InputStream2Bytes(Inp

Android中.9.png图片的使用过程和原理

1.Android中放置图片资源的文件夹 Android中一般有drawable-ldpi.drawable-mdpi.drawable-hdpi.drawable-xhdpi.drawable-xxhdpi等放置图片资源的文件夹,这几个文件夹分别对应的像素密度为: 文件夹 对应的像素密度 drawable-ldpi 120dpi drawable-mdpi 160dpi drawable-hdpi 240dpi drawable-xhdpi 320dpi 图-1 另外自己可以创建一个默认的dr

Android中屏幕密度和图片大小的关系分析

转发:http://blog.csdn.net/singwhatiwanna/article/details/19139013 前言 Android中支持许多资源,包括图片(Bitmap),对应于bitmap的文件夹是drawable,除了drawable,还有drawable-ldpi.drawable-mdpi.drawable-hdpi.drawable-xhdpi.drawable-xxhdpi等,同一张图片放到上面不同的文件夹中是有区别的,比如一张100 * 100像素大小的图片,分别

Android中各种Adapter的使用方法

1.概念 Adapter是连接后端数据和前端显示的适配器接口.是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都须要用到Adapter.例如以下图直观的表达了Data.Adapter.View三者的关系: Android中全部的Adapter一览: 由图能够看到在Android中与Adapter有关的全部接口.类的完整层级图. 在我们使用过程中能够依据自己的需求实现接口或者继承类进行一定的扩展.比較经常使用的有 BaseAdapter,Si