android 将图片转换成黑白图片

先调用android的系统工具,获取图片生成bitmap文件,再使用android中的二值化技术,将图片转化成黑白图片,主界面代码:

package com.example.blackwhite;

import java.io.IOException;

import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private final String IMAGE_TYPE = "image/*";

	private final int IMAGE_CODE = 0; // 这里的IMAGE_CODE是自己任意定义的

	private Button addPic = null;

	private ImageView imgShow = null;

	private TextView imgPath = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		// TODO Auto-generated method stub

		addPic = (Button) findViewById(R.id.btn_add);
		imgPath = (TextView) findViewById(R.id.img_path);
		imgShow = (ImageView) findViewById(R.id.imgShow);
		addPic.setOnClickListener(listener);

	}

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			Button btn = (Button) v;

			switch (btn.getId()) {

			case R.id.btn_add:
				setImage();
				break;
			}

		}

		private void setImage() {
			// TODO Auto-generated method stub
			// 使用intent调用系统提供的相册功能,使用startActivityForResult是为了获取用户选择的图片的地址
			Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
			getAlbum.setType(IMAGE_TYPE);
			startActivityForResult(getAlbum, IMAGE_CODE);
		}
	};

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		//  RESULT_OK 是系统自定义得一个常量
		if (resultCode != RESULT_OK) { 
			Log.e("onActivityResult", "返回的resultCode出错");
			return;
		}

		Bitmap bm = null;

		// 外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
		ContentResolver resolver = getContentResolver();

		// 判断接收的Activity是不是选择图片的
		if (requestCode == IMAGE_CODE) {

			try {
				// 获得图片的地址Uri
				Uri originalUri = data.getData(); 
				//根据图片的URi生成bitmap
				bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);
				//将图片转化成黑白图片
				bm =convertToBlackWhite(bm);
				//显得到bitmap图片
				imgShow.setImageBitmap(bm);

				// 新建一个字符串数组用于存储图片地址数据。
				String[] proj = { MediaStore.Images.Media.DATA };

				// android系统提供的接口,用于根据uri获取数据
				Cursor cursor = managedQuery(originalUri, proj, null, null,
						null);

				// 获得用户选择图片的索引值
				int column_index = cursor
						.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

				// 将游标移至开头 ,防止引起队列越界
				cursor.moveToFirst();

				// 根据索引值获取图片路径
				String path = cursor.getString(column_index);
				imgPath.setText(path);
			} catch (IOException e) {
				Log.e("getImg", e.toString());

			}
		}
	}
	
	/**
	* 将彩色图转换为纯黑白二色
	* 
	* @param 位图
	* @return 返回转换好的位图
	*/
	public static Bitmap convertToBlackWhite(Bitmap bmp) {
		int width = bmp.getWidth(); // 获取位图的宽
		int height = bmp.getHeight(); // 获取位图的高
		int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组

		bmp.getPixels(pixels, 0, width, 0, 0, width, height);
		int alpha = 0xFF << 24;
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				int grey = pixels[width * i + j];

				//分离三原色
				int red = ((grey & 0x00FF0000) >> 16);
				int green = ((grey & 0x0000FF00) >> 8);
				int blue = (grey & 0x000000FF);
				
				//转化成灰度像素
				grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
				grey = alpha | (grey << 16) | (grey << 8) | grey;
				pixels[width * i + j] = grey;
			}
		}
		//新建图片
		Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
		//设置图片数据
		newBmp.setPixels(pixels, 0, width, 0, 0, width, height);

		Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newBmp, 380, 460);
		return resizeBmp;
	}

}

示例代码地址: http://download.csdn.net/detail/stop_pig/8211315

你可能感兴趣的:(android,图片,黑白转换)