bitmap在Android开发中是最简单也是最常用的知识点,也是Android系统中重要的类之一,我们运用bitmap可以做相关的旋转、剪切、缩放、指定格式保存、压缩、调整图片的颜色阈值,处理图像等;今天有个群友问到一点有关bitmap的知识,意识到有必要对自己以前的知识做一个比较全面的总结,也是对自己负责。
一、有关bitmap常用的相关方法
public final int getWidth()//获取位图的宽度
public final int getHeight()//获取位图的高度
public int getScaledWidth(Canvas canvas)//获取指定密度转换后的图像的宽度
public int getScaledHeight(Canvas canvas)//获取指定密度转换后的图像的高度
public boolean compress(CompressFormat format, int quality, OutputStream stream)//按指定的图片格式以及画质,将图片转换为输出流。
format:Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG
quality:画质,0-100.0表示最低画质压缩,100以最高画质压缩。对于PNG等无损格式的图片,会忽略此项设置。
public void recycle() // 回收位图占用的内存空间,把位图标记为Dead
public final boolean isRecycled() //判断位图内存是否已释放
public final boolean isMutable()//图片是否可修改
public static Bitmap createBitmap(Bitmap src) //以src为原图生成不可变得新图像
public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)//以src为原图,创建新的图像,指定新图像的高宽以及是否可变。
public static Bitmap createBitmap(int width, int height, Config config)——创建指定格式、大小的位图
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)以source为原图,创建新的图片,指定起始坐标以及新图像的高宽。
BitmapFactory工厂类:
Option 参数类:
public boolean inJustDecodeBounds//如果设置为true,不获取图片,不分配内存,但会返回图片的高度宽度信息。
public int inSampleSize//图片缩放的倍数
public int outWidth//获取图片的宽度值
public int outHeight//获取图片的高度值
public int inDensity//用于位图的像素压缩比
public boolean inScaled//设置为true时进行图片压缩,从inDensity到inTargetDensity
public int inTargetDensity//用于目标位图的像素压缩比(要生成的位图)
public byte[] inTempStorage //创建临时文件,将图片存储
public boolean inDither //如果为true,解码器尝试抖动解码
public Bitmap.Config inPreferredConfig //设置解码器
public String outMimeType //设置解码图像
public boolean inPurgeable//当存储Pixel的内存空间在系统内存不足时是否可以被回收
public boolean inInputShareable //inPurgeable为true情况下才生效,是否可以共享一个InputStream
public boolean inPreferQualityOverSpeed //为true则优先保证Bitmap质量其次是解码速度
public boolean inMutable //配置Bitmap是否可以更改,比如:在Bitmap上隔几个像素加一条线段
public int inScreenDensity //当前屏幕的像素密度
工厂方法:
public static Bitmap decodeFile(String pathName)
public static Bitmap decodeFile(String pathName, Options opts) //从文件读取图片
public static Bitmap decodeResource(Resources res, int id) //从资源文件读取图片
public static Bitmap decodeResource(Resources res, int id, Options opts)
public static Bitmap decodeStream(InputStream is) //从输入流读取图片
public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts)
public static Bitmap decodeByteArray(byte[] data, int offset, int length) //从数组读取图片
public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts)
public static Bitmap decodeFileDescriptor(FileDescriptor fd)//从文件读取文件 与decodeFile不同的是这个直接调用JNI函数进行读取 效率比较高
public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts)
Bitmap.Config inPreferredConfig :
枚举变量 (位图位数越高代表其可以存储的颜色信息越多,图像越逼真,占用内存越大)
public static final Bitmap.Config ALPHA_8 //代表8位Alpha位图 每个像素占用1byte内存
public static final Bitmap.Config ARGB_4444 //代表16位ARGB位图 每个像素占用2byte内存
public static final Bitmap.Config ARGB_8888 //代表32位ARGB位图 每个像素占用4byte内存
public static final Bitmap.Config RGB_565 //代表8位RGB位图 每个像素占用2byte内存
Android中一张图片(BitMap)占用的内存主要和以下几个因数有关:图片长度,图片宽度,单位像素占用的字节数。一张图片(BitMap)占用的内存=图片长度*图片宽度*单位像素占用的字节数
/** * bitmap转成drawable * * @param resources * @param bitmap * @return */ public static Drawable bitmap2Drawable(Resources resources, Bitmap bitmap) { Drawable drawable = new BitmapDrawable(resources, bitmap); return drawable; } /** * drawable转换成bitmap * * @param drawable * @return */ public static Bitmap drawable2Bitmap(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } /** * bitmap转换成字节数组 * * @param bitmap * @return */ public static byte[] bitmap2Bytes(Bitmap bitmap) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); return outputStream.toByteArray(); } /** * 图片旋转角度 * * @param bitmap * @param degress * @return */ private static Bitmap rotateBitmap(Bitmap bitmap, float degress) { if (bitmap == null) return null; Matrix matrix = new Matrix(); Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); return rotateBitmap; } /** * 根据scale生成一张图片 * * @param bitmap * @param scale 等比缩放值 * @return */ public static Bitmap scaleBitmap(Bitmap bitmap, float scale) { Matrix matrix = new Matrix(); matrix.postScale(scale, scale); Bitmap scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); return scaleBitmap; } /** * 压缩图片 * * @param bitmap * @return * @throws IOException */ public static Bitmap compressBitmap(Bitmap bitmap) throws IOException { if (bitmap == null) return null; ByteArrayOutputStream bos = null; try { bos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); byte[] bitmapBytes = bos.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(bitmapBytes); Bitmap compressBitmap = BitmapFactory.decodeStream(bis); return compressBitmap; } catch (OutOfMemoryError error) { error.printStackTrace(); } finally { if (bos != null) bos.close(); } return null; } /** * bitmap文件保存成file文件 * * @param bitmap * @param path * @param quality */ public static void bitmapSave2File(Bitmap bitmap, String path, int quality) { File bitmapFile = new File(path); try { FileOutputStream fos = new FileOutputStream(bitmapFile); BufferedOutputStream bos = new BufferedOutputStream(fos);//缓存输出流 bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos); bos.flush(); bos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 获取bitmap从本地 * * @param mContext * @param filePath * @return */ public static Bitmap getBitmapFromAssetsFile(Context mContext, String filePath) { Bitmap bitmap = null; AssetManager assetManager = mContext.getResources().getAssets(); try { InputStream is = assetManager.open(filePath); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } /** * 读取照片exif信息中的旋转角度 * * @param path 图片中的路径 * @return 返回角度 */ private static int readPictureDegree(String path) { if (TextUtils.isEmpty(path)) return 0; int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; } /** * 从二进制数据读取图片 * * @param bitmapByte * @param width * @param height * @return */ public static Bitmap readBitmapFromByteArray(byte[] bitmapByte, int width, int height) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length, options); float bWidth = options.outWidth; float bHeight = options.outHeight; int inSampleSize = 1; if (bWidth > width || bHeight > height) { if (bWidth > bHeight) inSampleSize = Math.round(bHeight / bWidth); else inSampleSize = Math.round(bWidth / bHeight); } options.inJustDecodeBounds = false; options.inSampleSize = inSampleSize; return BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length, options); }