Bitmap详解之获取Bitmap的方法

Bitmap详解之获取Bitmap的方法

获取Bitmap

1.将文件转化成Bitmap

public static final float DISPLAY_WIDTH = 200;
public static final float DISPLAY_HEIGHT = 200;
/**
 * 从path中获取图片信息
 * @param path
 * @return
 */
private Bitmap decodeBitmap(String path){
  BitmapFactory.Options op = new BitmapFactory.Options();
  //inJustDecodeBounds 
  //If set to true, the decoder will return null (no bitmap), but the out…
  op.inJustDecodeBounds = true;
  Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息
  //获取比例大小
  int wRatio = (int)Math.ceil(op.outWidth/DISPLAY_WIDTH);
  int hRatio = (int)Math.ceil(op.outHeight/DISPLAY_HEIGHT);
  //如果超出指定大小,则缩小相应的比例
  if(wRatio > 1 && hRatio > 1){
    if(wRatio > hRatio){
      op.inSampleSize = wRatio;
    }else{
      op.inSampleSize = hRatio;
    }
  }
  op.inJustDecodeBounds = false;
  bmp = BitmapFactory.decodeFile(path, op);
  return bmp;
}

在通过BitmapFactory.decodeFile(String path)方法将突破转成Bitmap时,遇到大一些的图片,我们经常会遇到OOM(Out Of Memory)的问题。所以用到了我们上面提到的BitmapFactory.Options这个类。

1
public boolean inJustDecodeBounds

If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels. 
如果inJustDecodeBounds(boolean flag)的参数是true,则不返回bitmap,但是能获取bitmap的宽和高(op.outWidth和op.outWidth) 。

1
op.inSampleSize = ratio;

设置该属性能够实现图片的压缩

2.Bitmap对象也可以通过Stream二进制流获取

1
Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null,  bmpFactoryOptions);

3.View转化为Bitmap

//要在子线程内读取
public static Bitmap convertViewToBitmap(View view){
  //当所需要的drawingCache >系统所提供的最大DrawingCache值时,生成Bitmap就会出现问题,此时获取的Bitmap就为null。
  //所以需要修改所需的cache值
  view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
  view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
  //获取缓存
  view.setDrawingCacheEnabled(true);
  Bitmap bitmap = view.getDrawingCache();
  //清空画图缓冲区
  view.setDrawingCacheEnabled(false);
  return bitmap;
}

或者

public static Bitmap getBitmapFromView(View view)
{
  // Define a bitmap with the same size as the view
  Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
  // Bind a canvas to it
  Canvas canvas = new Canvas(returnedBitmap);
  // Get the view's background
  Drawable bgDrawable = view.getBackground();
  if (bgDrawable != null)
    // has background drawable, then draw it on the canvas
    bgDrawable.draw(canvas);
  else
    // does not have background drawable, then draw white background on
    // the canvas
    canvas.drawColor(Color.WHITE);
  // draw the view on the canvas
  view.draw(canvas);
  // return the bitmap
  return returnedBitmap;
}

或者

//图片不被压缩
public static Bitmap convertViewToBitmap(View view, int bitmapWidth, int bitmapHeight){
    Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
    view.draw(new Canvas(bitmap));

    return bitmap;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Javascript的bitmap处理,并且将位图输出为base64编码以便于浏览器进行显示。   一、Bitmap.create(width, height, bgcolor)     创建一个width x height像素大小的位图,底色为bgcolor所代表的颜色。     如:bitmap.create(10, 10, 0xff0000); // 创建一个10 x 10像素的底色为红色的位图 二、Bitmap.toBase64()     将位图输出为base64编码的带datauri头(data:image/bmp;base64,)的字符串,以便于在浏览器里显示。     如:document.getElementById('img1').src = bitmap.toBase64(); 三、Bitmap.fromBase64()     自图像的BASE64编码中恢复位图数据,目前只支持24位色的BMP位图数据。     如:bitmap.fromBase64('Qk06AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABABgAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=='); 四、Bitmap.setBitmapBytes(val, idx, length)     修改bitmap位图数据的第idx位置起的length字节为val值。 五、Bitmap.getBitmapBytes(idx, length)     获取bitmap位图数据的第idx位置起的length个字节的值,返回值为数组。 六、Bitmap.setHeaderValue(attribute, headerValue)     设置attribute头属性的值为headerValue,attribute必须为BitMapFormat的成员属性,需要提供offset、length等属性值。     如:bitmap.setHeaderValue(BitmapFormat.biWidth, 500); // 设置位图的宽度为500像素值 七、Bitmap.getHeaderValue(attribute)     获取位图attribute头属性的值,attribute必须为BitmapFormat的成员属性,需要提供offset、length等属性值,返回的是经过Endian转换后的实际整数值。 八、Bitmap.setPixel(x, y, color)     设置位图的(x, y)位置的像素值为color。 九、Bitmap.getPixel(x, y)     获取位图的(x, y)位置的RGB值,返回的内容为[ rr, gg, bb ]的数组内容 标签:jsBitmap

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值