当前位置:首页 > 开发 > 移动开发 > 正文

android 中Drawable和Bitmap转换及常用于图片相关操作方法

发表于: 2012-04-13   作者:1028826685   来源:转载   浏览:
摘要: public class ImageTools { //Drawable-->Bitmap public static Bitmap drawableToBitmap(Drawable drawable){ int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitm

public class ImageTools {
//Drawable-->Bitmap
public static Bitmap drawableToBitmap(Drawable drawable){
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0,0,width,height);
drawable.draw(canvas);
return bitmap;

}

//Bitmap--> Drawable
public static Drawable BitmapConvertToDrawale(Bitmap bitmap) {

// Bitmap bitmap = new Bitmap();
Drawable drawable = new BitmapDrawable(bitmap);
return drawable;
}

// Bitmap-->Drawable

/*
* public static Bitmap DrawableConvertToBitmap(long id) { Resources
* res=getResources();
*
* Bitmap bmp=BitmapFactory.decodeResource(,id);
*
* return bmp; }
*/
// Bitmap-->Byte
public static byte[] Bitmap2Bytes(Bitmap bm) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

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

return baos.toByteArray();
}

// Byte-->Bitmap
public static Bitmap Bytes2Bimap(byte[] b) {

if (b.length != 0) {

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

}

else {

return null;

}

}
//改变图片的颜色
public static Bitmap invert(Bitmap src) {
Bitmap output = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
src.getConfig());
//A表示透明度,R,G,B是RGB调色
int A, R, G, B;
int pixelColor;
int height = src.getHeight();
int width = src.getWidth();

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixelColor = src.getPixel(x, y);
A = Color.alpha(pixelColor);

R = 255 - Color.red(pixelColor);
G = 255 - Color.green(pixelColor);
B = 255 - Color.blue(pixelColor);

output.setPixel(x, y, Color.argb(A, R, G, B));
}
}

return output;
}

public static byte[] getImage(String urlpath) throws Exception {
URL url = new URL(urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(6 * 1000);
// 别超过10秒。
if(conn.getResponseCode()==200){
InputStream inputStream=conn.getInputStream();
return readStream(inputStream);
}
return null;
}

/**
* 读取数据
* 输入流
*
* */
public static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outstream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=-1;
while((len=inStream.read(buffer)) !=-1){
outstream.write(buffer, 0, len);
}
outstream.close();
inStream.close();

return outstream.toByteArray();
}
}

android 中Drawable和Bitmap转换及常用于图片相关操作方法

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号