您的位置:首页 > 移动开发 > Android开发

【Android开发】图形图像处理技术-Bitmap和BitmapFactory类

2015-04-21 22:09 567 查看
一、Bitmap类

Bitmap类代表位图,是Android系统中图像处理的一个重要类。使用该类,不仅可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,而且还可以指定格式保存图像文件。对于这些操作,都可以通过Bitmap类提供的方法来实现。Bitmap类提供的常用方法如表所示:

(1)public final int getHeight():获取位图宽度

(2)public final int getWidth():获取位图高度

(3)public static Bitmap createBitmap(Bitmap src):通过位图资源创建位图实例

(4)public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height):在指定位置创建位图,可放大缩小位图  source是位图资源,(x,y)表示位图左上角坐标,width表示位图宽度,height表示位图高度

(5)public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter):截取位图中指定区域中的位图  source是目标位图资源,(x,y)表示剪切位图左上角起始点坐标,width表示目标位图宽度,height表示目标位图高度,m表示选择区域,filter是当其为true且m表示的区域大于前面参数所描述的区域时就填充截取的位图空白处

(6)public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream):保存图像文件  format是图片保存格式,quality是图片压缩的质量取值在0-100之间 0表示压缩的最小尺寸 100表示压缩的最好质量,stream是图片的输出流

注:对于PNG格式的图片是有损压缩的,quality压缩图片是没有效果的,该参数的设置无影响

(7)creatScaleBitmap(Bitmap source, int x, int y, int width, int height, boolean filter)

用于将源位图缩放为指定宽度和高度的新的Bitmap对象

(8)isRecycled()用于判断Bitmap对象是否被回收

(9)recycle()强制回收Bitmap对象

例如,创建一个包括4个像素(每个像素对应一种颜色)的Bitmap对象的代码如下:
Bitmap bitmap=Bitmap.creatBitmap(new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.MAGENTA},
4,1,Config.RGB_565);


Bitmap小测试:

res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tableLayout1"
android:orientation="vertical"
>
<com.example.test.DrawView
android:id="@+id/drawView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>


DrawView.java:
package com.example.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Style;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class DrawView extends View{
/*
* 功能:构造方法
* */
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);

}

/*
* 功能:重写onDraw方法
* */
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint=new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(2);
paint.setStyle(Style.STROKE);
Bitmap bitmap=Bitmap.createBitmap(40,60,Config.RGB_565);
canvas.drawBitmap(bitmap, 150, 150,paint);
}
}

运行效果如图所示



二、BitmapFactory类

在Android中,还提供了一个Bitmap类,该类作为一个工具类,用于从不同的数据源来解析、创建Bitmap对象。BitmapFactory类提供的创建Bitmap对象的常用方法如表所示:

(1)decodeFile(String pathName)用于从给定的路径所指定的文件中解析、创建Bitmap对象

(2)decodeFileDescriptor(FileDescriptor fd)用于从FileDescriptor对应的文件中解析、创建Bitmap对象

(3)decodeResource(Resource res,int id)用于根据给定的资源id,从指定的资源中解析、创建Bitmap对象

(4)decodeStream(InputStream is)用于从指定的输入流中解析、创建Bitmap对象

例如,要解析SD卡上的图片文件img01.jpg并创建对应的Bitmap对象,可以使用下面的代码:

String path="/sdcard/pictrues/bccd/img01.jpg";

Bitmap bm=BitmapFactory.decodeFile(path);

要解析Drawable资源中保存的图片文件img02.jpg并创建对应的Bitmap对象,可以使用下面的代码:

Bitmap bm=BitmapFactory.decodeResoutce(MainActivity.this,R.drawable.img02.jpg);
这一点在之前写的"移动的小兔子"的实例中用到过。

转载请注明出处:http://blog.csdn.net/acmman/article/details/45177209
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: