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();
}
}

更多相关文章

  1. Android Button 点击时替换背景颜色和替换当前图标
  2. Android 字体颜色变化(点击)
  3. android 字体&颜色
  4. Android 字体和颜色
  5. android 解析服务端下发的颜色值
  6. Android之开发常用颜色
  7. Android Studio App设置TextView文字内容大小颜色
  8. Android中设置控件透明度的方法

随机推荐

  1. Android 进阶之 Android消息机制Handler
  2. Android NDK会带来什么,除去你对NDK的一些
  3. Android自动化工具Monkeyrunner使用(五)
  4. Android中Activity的四种启动模式详解
  5. Android Binder
  6. 安卓017ListView & GridView & ScrollView
  7. Android必备:Android Activity的生命周期
  8. Android日志输出单元测试Debug
  9. Android锁定横竖屏、splash,全屏、去除标
  10. Android TextView 中如何使用第三方字体