Android Bitmap 加载与像素操作

Android Bitmap 加载与像素操作

一:加载与像素读写 在Android SDK中,图像的像素读写可以通过getPixel与setPixel两个Bitmap的API实现。Bitmap API读取像素的代码如下:

green = Color.green(pixel); 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;((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++){green = Color.green(pixel); 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++){green = Color.green(pixel); 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++){green = Color.green(pixel); 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文件内容如下:

=====”com.example.imageprocess1.MainActivity” ><RelativeLayoutandroid:layout_width=”wrap_content”android:layout_height=”wrap_content”><Button=”wrap_content”android:text=”@string/process” /><Button==”@+id/button_gray_3″android:text=”@string/inverse” /><Button==”@+id/button_gray_3″android:text=”@string/nored” /><Button==”@string/noblue” /><Button==”@+id/button_gray_2″android:text=”@string/flip” /><Button==><ImageView==”@drawable/ic_launcher” /></LinearLayout>让我们将事前的忧虑,换为事前的思考和计划吧!

Android Bitmap 加载与像素操作

相关文章:

你感兴趣的文章:

标签云: