获取Bitmap对象

1、使用常用的静态方法获取Bitmap对象
static Bitmap     createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
//Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix.
static Bitmap     createBitmap(int width, int height, Bitmap.Config config)
//Returns a mutable bitmap with the specified width and height.
static Bitmap     createBitmap(Bitmap source, int x, int y, int width, int height)
//Returns an immutable bitmap from the specified subset of the source bitmap.
static Bitmap     createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
static Bitmap     createBitmap(Bitmap src)
//Returns an immutable bitmap from the source bitmap.
static Bitmap     createBitmap(int[] colors, int width, int height, Bitmap.Config config)
//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
static Bitmap     createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
//Creates a new bitmap, scaled from an existing bitmap, when possible.  

2、使用BitmapFactory工厂类获取Bitmap对象
BitmapFactory工厂类是一个工具类,包含了很多种获取Bitmap的方法。
1. Decode an immutable bitmap from the specified byte array.
static Bitmap   decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)  //  解析byte[]
static Bitmap decodeByteArray(byte[] data, int offset, int length)
2.Decode a file path into a bitmap.
static Bitmap   decodeFile(String pathName)
static Bitmap   decodeFile(String pathName, BitmapFactory.Options opts)
3. Decode a bitmap from the file descriptor.
static Bitmap decodeFileDescriptor(FileDescriptor fd)
static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts)
4. Synonym for opening the given resource
static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options opts)
//Synonym for opening the given resource and calling decodeResourceStream(Resources, TypedValue, InputStream, Rect, BitmapFactory.Options).
static Bitmap decodeResource(Resources res, int id)
//Synonym for decodeResource(Resources, int, android.graphics.BitmapFactory.Options) will null Options.
5. Decode a new Bitmap from an InputStream.
static Bitmap decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts)
static Bitmap decodeStream(InputStream is)
static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)

3、使用BitmapDrawable获取Bitmap对象,BitmapDrawable继承于Drawable
//方法一
Resources res;
InputStream is=res.openRawResource(R.drawable.pic);
BitmapDrawable bitmapDrawable=new BitmapDrawable(is);
Bitmap bmp=bitmapDrawable.getBitmap();
//方法二
Resources res;
BitmapDrawable bitmapDrawable=(BitmapDrawable)res.getDrawable(R.drawable.pic);
Bitmap bmp=bitmapDrawable.getBitmap();
//方法三
ImageView image;
image.setImageBitmap(BitmapFactory.decodeStream(~~~~));
BitmapDrawable bitmapDrawable=(BitmapDrawable)image.getDrawable();
Bitmap bmp=bitmapDrawable.getBitmap();

当在Android工程的Drawable文件夹中导入图像文件(GIF、PNG、JPG,BMP)时,Android SDK会为这个文件生成一个Drawable对象。可以通过R.drawable的方式访问这个对象。一般是调用Resource.getDrawable(int id)的方式直接获取。
4. Bitmap与Drawable的转换关系

  • Bitmap转为Drawable:BitmapDrawable bitmapDrawable= new BitmapDrawable(bitmap) /BtimapDrawable是Drawable的子类,最终直接使用bitmapDrawable即可。
  • Drawable转为Bitmap:

5.Bitmap与byte[]的转换关系

  • Bitmap2Bytes:

public byte[] Bitmap2Bytes(Bitmap bmp) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream)
bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream );
return byteArrayOutputStream.toByteArray();
}

  • Bytes2Bitmap:

static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)

via http://blog.csdn.net/conowen/article/details/7832789

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: