天天看点

android 图片处理总结



一、图片来源:

1.项目下的资源文件:

a.资源文件 res/drawable文件夹

b.assets文件夹 存储量较小

c.src目录下

2.网络加载

3.本地文件夹(SD卡)

二、获取方式:

a.id查找(资源文件夹下)

b.文件路径(网络路径,本地文件路径)

三、如何在代码中处理图片

a.Drawable

b.Bitmap

c.InputStream

d.byte[]

四、具体操作:

1. 图片放在sdcard中,

Bitmap imageBitmap = BitmapFactory.decodeFile(path) (path 是图

片的路径,跟目录是/sdcard)

2.res下获取bitmap

代码如下:

public Bitmap getRes(String name){

ApplicationInfo appInfo=getApplicationInfo();

int resID=getResources().getIdentifier

(name,"drawable",appInfo.packageName);//name是该图片的名

字,"drawable"是该图片存放的目录,appInfo.packageName是应用程

序的包名

return BitmapFactory.decodeResource(getResources(),resID);

}

3.图片放在src目录下

String path="com/yy/ss.png";//图片存放的路径

InputStream is=getClassLoader().getResourceAsStream(path);//得

到图片流

4.在assets下,只可以存放只读文件

InputStream is=getResources().getAssets().open(name);

5.sd卡

FileInputStreamfis = new FileInputStream(path);

BufferedInputStreambis = new BufferedInputStream(fis);

Bitmap bitmap= BitmapFactory.decodeStream(bis);

Bitmap newBit= Bitmap.createScaledBitmap(bitmap, 180, 180,

false);

6.网络加载图片

详见:http://www.devstore.cn/new/newInfo/1026.html

系列讲解

五、图片类型的相互转化

1.Bitmap与InputStream的相互转化

a.Bitmap-->InputStream

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

InputStream isBm = new ByteArrayInputStream(baos.toByteArray

());

b.InputStream --> Bitmap

public Bitmap InputStream2Bitmap(InputStream is) {

return BitmapFactory.decodeStream(is);

}

2.Bitmap与Drawable

// Drawable转换成Bitmap

public 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转换成Drawable

public Drawable bitmap2Drawable(Bitmap bitmap) {

BitmapDrawable bd = new BitmapDrawable(bitmap);

Drawable d = (Drawable) bd;

return d;

}

3.drawable与InputStream

// Drawable转换成InputStream

public InputStream Drawable2InputStream(Drawable d) {

Bitmap bitmap = this.drawable2Bitmap(d);

return this.Bitmap2InputStream(bitmap);

}

// InputStream转换成Drawable

public Drawable InputStream2Drawable(InputStream is) {

Bitmap bitmap = this.InputStream2Bitmap(is);

return this.bitmap2Drawable(bitmap);

}

4.drawable与byte[]

// Drawable转换成byte[]

public byte[] Drawable2Bytes(Drawable d) {

Bitmap bitmap = this.drawable2Bitmap(d);

return this.Bitmap2Bytes(bitmap);

}

// byte[]转换成Drawable

public Drawable Bytes2Drawable(byte[] b) {

Bitmap bitmap = this.Bytes2Bitmap(b);

return this.bitmap2Drawable(bitmap);

}

5.bitmap与byte[]

// Bitmap转换成byte[]

public byte[] Bitmap2Bytes(Bitmap bm) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

return baos.toByteArray();

}

// byte[]转换成Bitmap

public Bitmap Bytes2Bitmap(byte[] b) {

if (b.length != 0) {

return BitmapFactory.decodeByteArray(b, 0, b.length);

}

return null;

}

6.InputStream与byte[]

// 将byte[]转换成InputStream

public InputStream Byte2InputStream(byte[] b) {

ByteArrayInputStream bais = new ByteArrayInputStream(b);

return bais;

}

// 将InputStream转换成byte[]

public byte[] InputStream2Bytes(InputStream is) {

String str = "";

byte []readByte = new byte[1024];

int readCount = -1;

try {

while ((readCount = is.read(readByte,0,1024)) != -1) {

str += new String(readByte).trim();

}

return str.getBytes();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

工具类直接下载链接:http://download.csdn.net/detail/atsince/8336495

继续阅读