当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程之图片相关代码集锦

Android编程之图片相关代码集锦

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例总结了android编程之图片相关代码。分享给大家供大家参考,具体如下:

1. bitmap转化为字符串:

/** 
* @param 位图 
* @return 转化成的字符串 
*/ 
public static string bitmaptostring(bitmap bitmap) { 
 // 将bitmap转换成字符串 
 string string = null; 
 bytearrayoutputstream bstream = new bytearrayoutputstream(); 
 bitmap.compress(compressformat.png, 100, bstream); 
 byte[] bytes = bstream.tobytearray(); 
 string = base64.encodetostring(bytes, base64.default); 
 return string; 
} 

2.字符串转化为bitmap:

/** 
* @param string 字符串 
* @return 转化成的位图 
*/ 
public static bitmap stringtobitmap(string string) { 
  // 将字符串转换成bitmap类型 
  bitmap bitmap = null; 
  try { 
   byte[] bitmaparray; 
   bitmaparray = base64.decode(string, base64.default); 
   bitmap = bitmapfactory.decodebytearray(bitmaparray, 0, bitmaparray.length); 
  } catch (exception e) { 
   e.printstacktrace(); 
  } 
  return bitmap; 
} 

3.bitmap转化为drawable:

/** 
* @param bitmap bitmap位图图像 
* @return drawable 转换后的drawable对象 
*/ 
public static drawable bitmaptodrawable(bitmap bitmap) { 
 if (bitmap == null) 
  return null; 
 if (160 != bitmap.getdensity()) { 
  bitmap.setdensity(160); 
 } 
 return new bitmapdrawable(bitmap); 
} 

根据图片资源id获取drawable对象:

/** 
 * @param context 上下文 
 * @param id  图片的资源id 
 * @return drawable对象 
 */ 
public static drawable resourcetodrawable(context context,int id) { 
 return null == context ? null : bitmaptodrawable(bitmapfactory.decoderesource(context.getresources(), id)); 
} 

byte数组转换drawble对象:

/** 
 * @param bytes byte数组 
 * @return drawble对象 
 */ 
public static drawable bytearraytodrawable(byte[] bytes) { 
 return null == bytes ? null : bitmaptodrawable(bitmapfactory.decodebytearray(bytes, 0, bytes.length)); 
} 

4.drawable转化为bitmap:

/** 
* drawble对象转bitmap对象 
* @param drawable drawble对象 
* @return bitmap对象 
*/ 
public static bitmap drawabletobitmap(drawable drawable) { 
  return null == drawable ? null : ((bitmapdrawable) drawable).getbitmap(); 
} 

5.byte数组转换bitmap对象:

/** 
* @param bytes byte数组 
* @return bitmap对象 
*/ 
public static bitmap bytearraytobitmap(byte[] bytes) { 
  return null == bytes ? null : bitmapfactory.decodebytearray(bytes, 0, bytes.length); 
} 

6.图片去色,返回灰度图片(老式图片):

/** 
* @param bitmap 传入的bitmap 
* @return 去色后的图片bitmap对象 
*/ 
public static bitmap tograyscale(bitmap bitmap) { 
  int width,height; 
  height = bitmap.getheight(); 
  width = bitmap.getwidth(); 
  bitmap bmpgrayscale = bitmap.createbitmap(width, height, bitmap.config.rgb_565); 
  canvas c = new canvas(bmpgrayscale); 
  paint paint = new paint(); 
  colormatrix cm = new colormatrix(); 
  cm.setsaturation(0); 
  colormatrixcolorfilter f = new colormatrixcolorfilter(cm); 
  paint.setcolorfilter(f); 
  c.drawbitmap(bitmap, 0, 0, paint); 
  return bmpgrayscale; 
} 

7.对图片进行缩放:

/** 
* @param url   图片的路径 
* @param requiresize 缩放的尺寸 
* @return 缩放后的图片bitmap对象 
*/ 
public static bitmap getscaleimage(string url,int requiresize) { 
  bitmapfactory.options o = new bitmapfactory.options(); 
  // 此属性表示图片不加载到内存,只是读取图片的属性,包括图片的高宽 
  o.injustdecodebounds = true; 
  bitmapfactory.decodefile(url, o); 
  int width_tmp = o.outwidth,height_tmp = o.outheight; 
  int scale = 1; 
  while (true) { 
   if (width_tmp / 2 < requiresize || height_tmp / 2 < requiresize) 
    break; 
   width_tmp /= 2; 
   height_tmp /= 2; 
   scale *= 2; 
  } 
  bitmapfactory.options o2 = new bitmapfactory.options(); 
  o2.insamplesize = scale; 
  bitmap bmp = bitmapfactory.decodefile(url, o2); 
  return bmp; 
} 

8.获得图片的倒影,同时倒影渐变效果:

/** 
* @param bitmap 图片源 
* @return 处理后的图片bitmap对象 
*/ 
public static bitmap createmirro(bitmap bitmap) { 
  int width = bitmap.getwidth(); 
  int height = bitmap.getheight(); 
  int shadow_height = 15; 
  int[] pixels = new int[width * height]; 
  bitmap.getpixels(pixels, 0, width, 0, 0, width, height); 
  // shadow effect 
  int alpha = 0x00000000; 
  for (int y = 0; y < height; y++) { 
   for (int x = 0; x < width; x++) { 
    int index = y * width + x; 
    int r = (pixels[index] >> 16) & 0xff; 
    int g = (pixels[index] >> 8) & 0xff; 
    int b = pixels[index] & 0xff; 
    pixels[index] = alpha | (r << 16) | (g << 8) | b; 
   } 
   if (y >= (height - shadow_height)) { 
    alpha = alpha + 0x1f000000; 
   } 
  } 
  // invert effect 
  bitmap bm = bitmap.createbitmap(width, height, bitmap.config.argb_8888); 
  for (int y = 0; y < height; y++) { 
   bm.setpixels(pixels, y * width, width, 0, height - y - 1, width, 1); 
  } 
  return bitmap.createbitmap(bm, 0, 0, width, shadow_height); 
} 

9.保存图片到sdcard:

/** 
* @param imagepath 图片保存路径 
* @param bm 被保存的bitmap对象 
*/ 
public static void saveimgtolocal(string imagepath, bitmap bm) { 
  if (bm == null || imagepath == null || "".equals(imagepath)) { 
   return; 
  } 
  file f = new file(imagepath); 
  if (f.exists()) { 
   return; 
  } else { 
   try { 
    file parentfile = f.getparentfile(); 
    if (!parentfile.exists()) { 
     parentfile.mkdirs(); 
    } 
    f.createnewfile(); 
    fileoutputstream fos; 
    fos = new fileoutputstream(f); 
    bm.compress(bitmap.compressformat.png, 100, fos); 
    fos.close(); 
   } catch (filenotfoundexception e) { 
    f.delete(); 
    e.printstacktrace(); 
   } catch (ioexception e) { 
    e.printstacktrace(); 
    f.delete(); 
   } 
  } 
}

10.从sdcard中获取图片:

/** 
* @param imagepath 图片在sdcard中保存的路径 
* @return 返回保存的bitmap对象 
*/ 
public static bitmap getimagefromlocal(string imagepath) { 
  file file = new file(imagepath); 
  if (file.exists()) { 
   bitmap bitmap = bitmapfactory.decodefile(imagepath); 
   file.setlastmodified(system.currenttimemillis()); 
   return bitmap; 
  } 
  return null; 
}

11.图片压缩处理:

/** 
* 对图片进行压缩,主要是为了解决控件显示过大图片占用内存造成oom问题。 
* 一般压缩后的图片大小应该和用来展示它的控件大小相近。 
* @param context 上下文 
* @param resid 图片资源id 
* @param reqwidth 期望压缩的宽度 
* @param reqheight 期望压缩的高度 
* @return 压缩后的图片 
*/ 
public static bitmap compressbitmapfromresourse(context context, int resid, int reqwidth, int reqheight) { 
  final bitmapfactory.options options = new bitmapfactory.options(); 
  /* 
   * 第一次解析时,injustdecodebounds设置为true, 
   * 禁止为bitmap分配内存,虽然bitmap返回值为空,但可以获取图片大小 
   */ 
  options.injustdecodebounds = true; 
  bitmapfactory.decoderesource(context.getresources(), resid, options); 
  final int height = options.outheight; 
  final int width = options.outwidth; 
  int insamplesize = 1; 
  if (height > reqheight || width > reqwidth) { 
   final int heightratio = math.round((float) height / (float) reqheight);
   final int widthratio = math.round((float) width / (float) reqwidth);
   insamplesize = heightratio < widthratio ? heightratio : widthratio;
  } 
  options.insamplesize = insamplesize; 
  //使用计算得到的insamplesize值再次解析图片 
  options.injustdecodebounds = false; 
  return bitmapfactory.decoderesource(context.getresources(), resid, options); 
}

12. 获取可用内存的最大值(app使用内存超出这个值会引起outofmemory异常):

private int getmaxmemoryforapp() { 
  int maxmemory = (int) (runtime.getruntime().maxmemory() / 1024); 
  return maxmemory; 
}

13.将图片裁剪成圆圈:

/** 
* 将bitmap处理为圆形的图片 
* @param bitmap 处理之前的位图 
* @return 处理之后的位图 
*/ 
public static bitmap circlepic(bitmap bitmap){ 
  int width = bitmap.getwidth(); 
  int height = bitmap.getheight(); 
  int r = width < height ? width/2:height/2;//圆的半径,取宽和高中较小的,以便于显示没有空白 
  bitmap outbitmap = bitmap.createbitmap(r*2, r*2, bitmap.config.argb_8888);//创建一个刚好2r大小的bitmap 
  canvas canvas = new canvas(outbitmap); 
  final int color =0xff424242; 
  final paint paint = new paint(); 
  /** 
   * 截取图像的中心的一个正方形,用于在原图中截取 
   * 坐标如下: 
   * 1.如果 w < h , 左上坐标(0, (h-w)/2) , 右上坐标(w, (h+w)/2) 偏移10 
   * 2.如果 w > h , 左上坐标((w-h)/2, 0) , 右上坐标((w+h)/2, h) 偏移10 
   */ 
  final rect rect = new rect( width < height ? 0 : (width-height)/2, width < height ? (height-width)/2 - 10 : -10, 
    width < height ? width : (width+height)/2, (width < height ? (height+width)/2 - 10: height - 10)); 
  //创建一个直径大小的正方形,用于设置canvas的显示与设置画布截取 
  final rect rect2 = new rect( 0, 0, r*2, r*2); 
  //提高精度,用于消除锯齿 
  final rectf rectf = new rectf(rect2); 
  //下面是设置画笔和canvas 
  paint.setantialias(true); 
  canvas.drawargb(0,0,0,0); 
  paint.setcolor(color); 
  //设置圆角,半径都为r,大小为rect2 
  canvas.drawroundrect(rectf, r, r, paint); 
  //设置图像重叠时的显示方式 
  paint.setxfermode(new porterduffxfermode(porterduff.mode.src_in)); 
  //绘制图像到canvas 
  canvas.drawbitmap(bitmap, rect, rect2, paint); 
  return outbitmap; 
 } 
}

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网