热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

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

这篇文章主要介绍了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 

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 > 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 

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 

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  h , 左上坐标((w-h)/2, 0) , 右上坐标((w+h)/2, h) 偏移10 
   */ 
  final Rect rect = new Rect( width 

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


推荐阅读
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 【Windows】实现微信双开或多开的方法及步骤详解
    本文介绍了在Windows系统下实现微信双开或多开的方法,通过安装微信电脑版、复制微信程序启动路径、修改文本文件为bat文件等步骤,实现同时登录两个或多个微信的效果。相比于使用虚拟机的方法,本方法更简单易行,适用于任何电脑,并且不会消耗过多系统资源。详细步骤和原理解释请参考本文内容。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
author-avatar
rsidugjig
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有