android图片二值化并显示在ImageView

libin96 2012-04-18 06:54:15

请各位大侠帮忙。是个按钮的监听实现。下面得到图片的方法一和二 结果是一样的吗??
ImageView上只显示Bitmap格式的图片吗???
怎么把像素数组转化成Bitmap并显示在ImageView上??
万分感谢!!


/*得到图片的大小 一*/
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;//dont save pic to memory ,only get width and height
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
//the resulting width and height of the bitmap
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;

/*得到图片的大小 二*/

BufferedImage image = null;
File f = new File(mCurrentPhotoPath);

try {
image=ImageIO.read(f);
} catch (IOException e) {

e.printStackTrace();
}
int iw = image.getWidth();
int ih = image.getHeight();
// 上面两种方法得到的图片的大小是一样的吗??

/*存储图片*/

int pixels [] = new int [iw*ih]; //如果想存储到二位数组中,怎么实现呢??

/*图片做黑白处理*/
int grey = 100;
// 对图像进行二值化处理,Alpha值保持不变
ColorModel cm = ColorModel.getRGBdefault();
for (int i = 0; i < iw * ih; i++) {
int red, green, blue;
int alpha = cm.getAlpha(pixels);
if (cm.getRed(pixels) > grey) {
red = 255;
} else {
red = 0;
}
if (cm.getGreen(pixels) > grey) {
green = 255;
} else {
green = 0;

}
if (cm.getBlue(pixels) > grey) {
blue = 255; } else { blue = 0; }
pixels = alpha << 24 | red << 16 | green << 8 | blue;

/*图片显示在ImageView上*/
bmOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
bitmap.setPixels(pixels, 0, photoW, 0, 0, photoW, photoH);

mImageView.setImageBitmap(bitmap);
mImageView.setVisibility(View.VISIBLE);




一点按钮就异常退出了
































































高级模式
B Color Image Link Quote Code Smilies |添加附件


.










发表回复 回帖后跳转到最后一页









. .



...全文
1117 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
为什么不用JNI处理呢,那样效率也高
libin96 2012-04-19
  • 打赏
  • 举报
回复
android可以导awt这个包用。。我想在android上做图像处理。。方法1和方法2得到图片的高和宽是一样的吗??

是不是昨晚图像处理后把bmOptions.inJustDecodeBounds = false;后得到图片。显示呢? 显示图片那代码正确吗? 就是显示不了
metapipe 2012-04-18
  • 打赏
  • 举报
回复
方法1:没有真正载入图片数据,只是得到宽和高
bmOptions.inJustDecodeBounds = true;//dont save pic to memory ,only get width and height

方法2:BufferedImage是java awt的方法,不是android的。

看下这个例子吧:
http://download.csdn.net/detail/sdsunqian/4128949

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧