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

   2015-11-25 0
核心提示:这篇文章主要介绍了Android编程之图片相关代码集锦,实例总结了大量Android图片操作相关代码,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例总结了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程序设计有所帮助。

 
标签: Android 图片
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 说一说Android Studio和IDEA中一个很有用的内存调试插件
    说一说Android Studio和IDEA中一个很有用的内存
    JetBrains JVM Debugger Memory View plugin 在我最近的研发活动期间寻找新的工具,以提高我的开发经验,使Android Studio的生活更轻松,我发现一个有用的插件,我从来没有听说过。 这就是为什么,我决定写这个强大的工具,它如何帮助我与内存调试我的应用程
  • 安卓中通知功能的具体实现
    安卓中通知功能的具体实现
    通知[Notification]是Android中比较有特色的功能,当某个应用程序希望给用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知实现。使用通知的步骤1、需要一个NotificationManager来获得NotificationManager manager = (NotificationManager
    02-05 安卓开发
  • Android view系统分析-setContentView
    Android view系统分析-setContentView
    第一天上班,列了一下今年要学习的东西。主要就是深入学习Android相关的系统源代码,夯实基础。对于学习Android系统源代码,也没什么大概,就从我们平常使用最基础的东西学起,也就是从view这个切入点开始学习Android的源码,在没分析源码之前,我们有的时候
    02-05 安卓开发
  • 如何进行网络视频截图/获取视频的缩略图
    如何进行网络视频截图/获取视频的缩略图
    小编导读:获取视频的缩略图,截图正在播放的视频某一帧,是在音视频开发中,常遇到的问题。本文是主要用于点播中截图视频,同时还可以获取点播视频的缩略图进行显示,留下一个问题,如下图所示, 如果要获取直播中节目视频缩略图,该怎么做呢?(ps:直播是直
  • Android NDK 层发起 HTTP 请求的问题及解决
    Android NDK 层发起 HTTP 请求的问题及解决
    前言新的一年,大家新年快乐~~鸡年大吉!本次给大家带来何老师的最新文章~虽然何老师还在过节,但依然放心不下广大开发者,在此佳节还未结束之际,给大家带来最新的技术分享~ 事件的起因不说了,总之是需要实现一个 NDK 层的网络请求。为了多端适用,还是选择
  • SDK热更之如何在SDK代码中自动插桩及如何生成补
    写在前面本文是SDKHotfix相关的SDK热更系列文章中的一篇,以下为项目及系列文章相关链接:SDKHotfix整体介绍:http://blog.bihe0832.com/sdk_hotfix_project.htmlSDKHotfix对应github地址:https://github.com/bihe0832/SDKHoxFix这篇文章主要介绍一下SDK热更
  • 安装量破千万的第一个产品,我总结了3句话
    安装量破千万的第一个产品,我总结了3句话
    在今天的文章中,作者回顾了自己的第一个产品,他说“我做的第一款产品,是我的一块里程碑。”一起来看看~背景老牌大型互联网公司,部门内部创业的一个项目。我作为产品经理,也是第一次做产品经理,主导产品项目。实际上,项目初期包括我和安卓开发2个人。开
  • 移动周刊第 176 期:Android 知识梳理
    移动周刊第 176 期:Android 知识梳理
    写在前面 本期移动周刊第 176 期如约而至,聚焦 Android、iOS、VR/AR/MR、直播等前沿移动开发技术,收录一周最热点,解读开发技巧,每周三移动周刊抢先看,我们希望从中能够让你有一些收获,如果你有好的文章以及优化建议,请发送邮件至mobilehub@csdn.net,
  • Android插件化(六): OpenAtlasの改写aapt以防止资源ID冲突
    Android插件化(六): OpenAtlasの改写aapt以防
    引言Android应用程序的编译中,负责资源打包的是aapt,如果不对打包后的资源ID进行控制,就会导致插件中的资源ID冲突。所以,我们需要改写aapt的源码,以达到通过某种方式传递资源ID的Package ID,通过aapt打包时获取到这个Package ID并且应用才插件资源的命名
    02-05 安卓开发
  • Android架构(一)MVP架构在Android中的实践
    Android架构(一)MVP架构在Android中的实践
    为什么要重视程序的架构设计 对程序进行架构设计的原因,归根结底是为了 提高生产力 。通过设计是程序模块化,做到模块内部的 高聚合 和模块之间的 低耦合 (如依赖注入就是低耦合的集中体现)。 这样做的好处是使得程序开发过程中,开发人员主需要专注于一点,
    02-05 安卓开发
点击排行