双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

android图片二值化并显示在ImageView

libin96 libin96 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

请各位大侠帮忙。是个按钮的监听实现。下面得到图片的方法一和二 结果是一样的吗??
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 |添加附件


.










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









. .



...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
libin96
Android 80,489 成员 91,375 内容