Android Bitmap 载入与像素操作

一:载入与像素读写
在Android SDK中,图像的像素读写能够通过getPixel与setPixel两个Bitmap的API实现。

Bitmap API读取像素的代码例如以下:

int pixel = bitmap.getPixel(col, row);// ARGB
int red = Color.red(pixel); // same as (pixel >> 16) &0xff
int green = Color.green(pixel); // same as (pixel >> 8) &0xff
int blue = Color.blue(pixel); // same as (pixel & 0xff)
int alpha = Color.alpha(pixel); // same as (pixel >>> 24)

得到像素pixel是32位的整数,四个字节分别相应透明通道、红色、绿色、蓝色通道。

Bitmap API 写入像素,代码例如以下:

bm.setPixel(col, row, Color.argb(alpha, red, green, blue));

通过Color.argb又一次组装成一个int的像素值。

使用BitmapFactory.decodeFile或者decodeResource等方法实现载入图像的Bitmap对象时。这些方法就会为要构建的Bitmap对象分配合适大小的内存。假设原始的图像文件数据非常大,就会导致DVM不能分配请求的内存大小。从而导致OOM(out of memory)问题。而通过配置BitmapFactory.Option预先读取图像高度与宽带,图像进行适当的下採样,就能够避免OOM问题的发生。预先仅仅获取图像高度与宽带的代码例如以下:

        // 获取Bitmap图像大小与类型属性BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(getResources(), R.drawable.shar_03, options);int height = options.outHeight;int width = options.outWidth;String imageType = options.outMimeType;

基于下採样载入超大Bitmap图像的缩小版本号:

        // 下採样int inSampleSize = 1;if (height > reqHeight || width > reqWidth) {final int halfHeight = height / 2;final int halfWidth = width / 2;// Calculate the largest inSampleSize value // that is a power of 2 and keeps both// height and width larger than the requested height and width.while ((halfHeight / inSampleSize) > reqHeight&& (halfWidth / inSampleSize) > reqWidth) {inSampleSize *= 2;}}// 获取採样后的图像显示。避免OOM问题options.inJustDecodeBounds = false;srcImage = BitmapFactory.decodeResource(getResources(), R.drawable.shar_03, options);

二:像素操作
android彩色图像灰度化的三个简单方法
灰度化方法一:
灰度值GRAY = (max(red, green, blue) + min(red, green, blue))/2
灰度化方法二:
灰度值GRAY = (red + green + blue)/3
灰度化方法三:
灰度值GRAY = red*0.3 + green*0.59 + blue*0.11
代码实现例如以下:

public Bitmap gray(Bitmap bitmap, int schema)
{Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());int width = bitmap.getWidth();int height = bitmap.getHeight();for(int row=0; row<height; row++){for(int col=0; col<width; col++){int pixel = bitmap.getPixel(col, row);// ARGBint red = Color.red(pixel); // same as (pixel >> 16) &0xffint green = Color.green(pixel); // same as (pixel >> 8) &0xffint blue = Color.blue(pixel); // same as (pixel & 0xff)int alpha = Color.alpha(pixel); // same as (pixel >>> 24)int gray = 0;if(schema == 0){gray = (Math.max(blue, Math.max(red, green)) + Math.min(blue, Math.min(red, green))) / 2;}else if(schema == 1){gray = (red + green + blue) / 3;}else if(schema == 2){gray = (int)(0.3 * red + 0.59 * green + 0.11 * blue);}bm.setPixel(col, row, Color.argb(alpha, gray, gray, gray));}}return bm;
}

Bitmap图像镜像映射与亮度调整的代码实现例如以下:

public Bitmap brightness(Bitmap bitmap, double depth)
{Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());int width = bitmap.getWidth();int height = bitmap.getHeight();for(int row=0; row<height; row++){for(int col=0; col<width; col++){int pixel = bitmap.getPixel(col, row);// ARGBint red = Color.red(pixel); // same as (pixel >> 16) &0xffint green = Color.green(pixel); // same as (pixel >> 8) &0xffint blue = Color.blue(pixel); // same as (pixel & 0xff)int alpha = Color.alpha(pixel); // same as (pixel >>> 24)double gray = (0.3 * red + 0.59 * green + 0.11 * blue);red += (depth * gray);if(red > 255) { red = 255; }green += (depth * gray);if(green > 255) { green = 255; }blue += (depth * gray);if(blue > 255) { blue = 255; }bm.setPixel(col, row, Color.argb(alpha, red, green, blue));}}return bm;
}public Bitmap flip(Bitmap bitmap)
{Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());int width = bitmap.getWidth();int height = bitmap.getHeight();for(int row=0; row<height; row++){for(int col=0; col<width; col++){int pixel = bitmap.getPixel(col, row);// ARGBint red = Color.red(pixel); // same as (pixel >> 16) &0xffint green = Color.green(pixel); // same as (pixel >> 8) &0xffint blue = Color.blue(pixel); // same as (pixel & 0xff)int alpha = Color.alpha(pixel); // same as (pixel >>> 24)int ncol = width - col - 1;bm.setPixel(ncol, row, Color.argb(alpha, red, green, blue));}}return bm;
}

执行截图:

布局XML文件内容例如以下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.imageprocess1.MainActivity" ><RelativeLayout
        android:layout_width="wrap_content"android:layout_height="wrap_content"><Button
            android:id="@+id/button_gray_3"android:layout_width="100dp"android:layout_height="wrap_content"android:text="@string/process" /><Button
            android:id="@+id/button_inverse"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_toRightOf="@+id/button_gray_3"android:layout_alignTop="@+id/button_gray_3"android:text="@string/inverse" /><Button
            android:id="@+id/button_gray_1"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_toRightOf="@+id/button_inverse"android:layout_alignTop="@+id/button_gray_3"android:text="@string/nored" /><Button
            android:id="@+id/button_gray_2"android:layout_width="100dp"android:layout_below="@+id/button_gray_3"android:layout_height="wrap_content"android:text="@string/noblue" /><Button
            android:id="@+id/button_flip"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_below="@+id/button_inverse"android:layout_toRightOf="@+id/button_gray_2"android:text="@string/flip" /><Button
            android:id="@+id/button_save"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_below="@+id/button_gray_3"android:layout_toRightOf="@+id/button_flip"android:text="@string/save" /></RelativeLayout><ImageView
        android:id="@+id/image_content"android:layout_width="fill_parent"android:layout_height="fill_parent"android:scaleType="fitCenter"android:src="@drawable/ic_launcher" /></LinearLayout>

MainActivity中的onCreate方法的代码例如以下:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iView = (ImageView) this.findViewById(R.id.image_content);
Bitmap b = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
Canvas c = new Canvas(b);
c.drawText("Load Image from here...", 50, 200, paint);
iView.setImageBitmap(b);
Button saveBtn = (Button) this.findViewById(R.id.button_save);
saveBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {Toast toast = Toast.makeText(getApplicationContext(), "Please load the image firstly...", Toast.LENGTH_SHORT);toast.show();loadImage();ImageView iView = (ImageView) findViewById(R.id.image_content);iView.setImageBitmap(srcImage);if(srcImage != null){//saveFile(srcImage);}}});
Button processBtn = (Button) this.findViewById(R.id.button_gray_3);
processBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {if(srcImage == null){loadImage();}ImagePixelsProcessor processor = new ImagePixelsProcessor();Bitmap bm = processor.gray(srcImage, 2); // 有不同的灰度化策略final ImageView iView = (ImageView) findViewById(R.id.image_content);iView.setImageBitmap(bm);}});Button inverseBtn = (Button) this.findViewById(R.id.button_inverse);
inverseBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {if(srcImage == null){loadImage();}ImagePixelsProcessor processor = new ImagePixelsProcessor();Bitmap bm = processor.brightness(srcImage, 0.3);final ImageView iView = (ImageView) findViewById(R.id.image_content);iView.setImageBitmap(bm);}
});Button noRedBtn = (Button) this.findViewById(R.id.button_gray_1);
noRedBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {if(srcImage == null){loadImage();}ImagePixelsProcessor processor = new ImagePixelsProcessor();Bitmap bm = processor.gray(srcImage, 0); // 有不同的灰度化策略final ImageView iView = (ImageView) findViewById(R.id.image_content);iView.setImageBitmap(bm);}
});Button gray2Btn = (Button) this.findViewById(R.id.button_gray_2);
gray2Btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {if(srcImage == null){loadImage();}ImagePixelsProcessor processor = new ImagePixelsProcessor();Bitmap bm = processor.gray(srcImage, 1); // 有不同的灰度化策略final ImageView iView = (ImageView) findViewById(R.id.image_content);iView.setImageBitmap(bm);}
});Button flipBtn = (Button) this.findViewById(R.id.button_flip);
flipBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {if(srcImage == null){loadImage();}ImagePixelsProcessor processor = new ImagePixelsProcessor();Bitmap bm = processor.flip(srcImage);final ImageView iView = (ImageView) findViewById(R.id.image_content);iView.setImageBitmap(bm);}
});

-第一次尝试用CSDN-markdown编辑器写文章。发现这个东西真好用!赞。。!
-gloomyfish@2015-07-02

转载于:https://www.cnblogs.com/mfrbuaa/p/5068162.html

Android Bitmap 载入与像素操作相关推荐

  1. Android Bitmap 加载与像素操作

    Android Bitmap 加载与像素操作 转载于:https://www.cnblogs.com/zhujiabin/p/4619049.html

  2. android都图片mat_计算机视觉 OpenCV Android | Mat像素操作(图像像素的读写、均值方差、算术、逻辑等运算、权重叠加、归一化等操作)...

    本文目录 1. 像素读写 2. 图像通道与均值方差计算 3. 算术操作与调整图像的亮度和对比度 4. 基于权重的图像叠加 5. Mat的其他各种像素操作 1. 像素读写 Mat作为图像容器,其数据部分 ...

  3. android获取区域内像素坐标,Android利用BitMap获得图片像素数据的方法

    本文实例讲述了Android利用BitMap获得图片像素数据的方法.分享给大家供大家参考,具体如下: 网上看到的参考是: int[] pixels = new int[bit.getWidth()*b ...

  4. android bitmap转图片_这是一份面向Android开发者的复习指南

    来自:简书,作者:九心 链接:https://www.jianshu.com/p/b3c1b9c6dd40 前言 相信很多同学都会有这样的感受,前三天刚刚复习的知识点,今天问的时候怎么就讲不出个所以然 ...

  5. Android bitmap图片处理

    一.View转换为Bitmap         在Android中所有的控件都是View的直接子类或者间接子类,通过它们可以组成丰富的UI界面.在窗口显示的时候Android会把这些控件都加载到内存中 ...

  6. Android Bitmap 研究与思考(上篇)

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/107951273 本文出自[赵彦军的博客] 做Android 6年来,一直都没有对 ...

  7. Android—Bitmap图片大小计算、压缩与三级缓存

    Bitmap对象占用内存大小: bitmap.getByteCount() 图片所占内存大小计算方式:图片长度 x 图片宽度 x 一个像素点占用的字节数. Android Bitmap使用的三种颜色格 ...

  8. android bitmap对比,Android Bitmap和Drawable的对比

    Android Bitmap和Drawable的对比 Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565.RGB888.作为一种逐像素的显示对象执行效率高,但 ...

  9. (4.6.31)Android Bitmap 详解

    文章目录 一.从相册加载一张图片 1.1 打开相册加载图片 1.2 根据Uri得到Bitmap 二.Bitmap 内存计算方式 2.1 density 和 densityDpi 2.2 getByte ...

  10. Android Bitmap实战技巧

    Android Bitmap实战技巧 http://www.cnblogs.com/punkisnotdead/p/4881771.html 注:本文大量参考谷歌官方文档自http://develop ...

最新文章

  1. 削减成本同时,多数企业仍计划增加云计算投入
  2. centos7下ip转发的配置
  3. 大数据可视化html模板开源_5个最受工程师欢迎的大数据可视化工具
  4. C# 网络编程之豆瓣OAuth2.0认证详解和遇到的各种问题及解决
  5. 128位计算机 ps2,64位就是最强电脑?难道就没有128位的电脑吗
  6. jsp网页实现任意进制的数转换成任意进制数
  7. 稀疏编码(sparse code)与字典学习(dictionary learning)
  8. 【转】 Pro Android学习笔记(八一):服务(6):复杂数据Parcel
  9. 离散数学与计算机网络的关系,离散数学与人工智能的关系.pdf
  10. Word控件Spire.Doc 【页面设置】教程(1):在C#/VB.NET:在 Word 文档中插入分页符
  11. 黑科技智能家电新生儿“智能冰箱”
  12. 卡内基梅隆大学计算机科学博士,卡内基梅隆大学有哪些专业处于世界顶尖水平?...
  13. 合理使用AutoHotKey+StrokeIt
  14. python報錯: OSError: Unable to locate Ghostscript on paths
  15. C/C++获取系统IP地址
  16. 【JSTL】JSP 标准标签库JSTL学习
  17. 用计算机做路由器,用笔记本做无线路由(笔记本电脑当无线路由器用怎么设置)...
  18. U盘安装ubuntu 16.04 遇到 gfxboot.c32:not a COM32R image boot 的解决方法
  19. Python数据结构之算法引入
  20. [安洵杯 2019]Attack (详细解析)

热门文章

  1. GIS-python学习
  2. 25本《Python+TensorFlow机器学习实战》免费包邮到家!
  3. 3.2 决策树可视化
  4. 深度学习2.08.tensorflow的高阶操作之张量排序
  5. argparse模块用法
  6. Android系统源代码情景分析
  7. 如何快速地真正的融入IT行业
  8. 英文怎么读_数学公式的英文读法
  9. js 对一个字段去重_写一个N-API没那么难?
  10. mysql 输入密码后闪退_iPhone抹除还原后需要输入账号密码怎么办?