我正在尝试构建简单的android项目以将图像转换为二进制格式,但是当我检查像素值时,我只得到零。我不知道问题出在哪里。任何人都可以帮助我。这是我的代码
iv = (ImageView) findViewById (R.id.uploadImage);
//convert the image view into bitmap
iv.buildDrawingCache();
bmap = iv.getDrawingCache();
int red;
int newPixel = 0;
int threshold =230;
int width = bmap.getWidth();
int hieght = bmap.getHeight();
int[] pix = new int[width * hieght];
int[] pix2 = new int[width * hieght];
int alpha = 0;
bmap.getPixels(pix, 0, width, 0, 0, width, hieght);
//convert the image into white and black
for (int i=0;i<width;i++) {
for (int j=0;j<hieght;j++) {
int index = j * width + i;
red = (pix[index] >> 16) & 0xff;
alpha = (pix[index] >> 24) & 0xff;
if (red<threshold) {
newPixel = 0;
} else {
newPixel = 255;
}
newPixel = colorToRGB(alpha, newPixel, newPixel, newPixel);
bmap.setPixel(i, j, newPixel);
iv.setImageBitmap(bmap);
}
}
//convert the black and white image view into bitmap
bmap.getPixels(pix2, 0, bmap.getWidth(), 0, 0, bmap.getWidth(),bmap.getHeight());
StringBuilder builder = new StringBuilder();
for (int i :pix2) {
builder.append(" " + i + " ");
}
Toast.makeText(this, builder, Toast.LENGTH_LONG).show();
private static int colorToRGB (int alpha, int red, int green, int blue) {
int newPixel = 0;
newPixel += alpha;
newPixel = newPixel << 8;
newPixel += red; newPixel = newPixel << 8;
newPixel += green; newPixel = newPixel << 8;
newPixel += blue;
return newPixel;
}
这是logcat
08-12 08:52:32.887:D / dalvikvm(774):GC_CONCURRENT释放71K,7%免费 2765K / 2964K,暂停23ms + 15ms,总计94ms
08-12 08:52:33.497:D / gralloc_goldfish(774):没有GPU的仿真器 检测到仿真。
08-12 08:52:40.698:D / dalvikvm(774):GC_CONCURRENT释放63K,6%免费 3087K / 3272K,暂停73ms + 3ms,总计165ms
08-12 08:52:49.397:I / Choreographer(774):跳过39帧!该 应用程序可能在其主线程上做了太多工作。
08-12 08:52:54.568:D / dalvikvm(774):GC_FOR_ALLOC释放了9K,5%免费 3124K / 3272K,暂停68ms,总共81ms
08-12 08:52:54.617:I / dalvikvm-heap(774):将堆(frag case)增长到 用于3145744字节分配的6.166MB
08-12 08:52:54.778:D / dalvikvm(774):GC_FOR_ALLOC获得40K,4%免费 6155K / 6348K,暂停156ms,总计156ms
08-12 08:52:55.008:D / dalvikvm(774):GC_CONCURRENT获释<1K,4%免费 6155K / 6348K,暂停80ms + 38ms,总计231ms
08-12 08:52:55.898:I / Choreographer(774):跳过412帧!该 应用程序可能在其主线程上做了太多工作。
08-12 08:52:57.898:D / dalvikvm(774):GC_FOR_ALLOC获得38K,4%免费 6119K / 6348K,暂停28ms,总计30ms
08-12 08:52:57.929:I / dalvikvm-heap(774):将堆(frag case)增长到 用于3145744字节分配的9.091MB
08-12 08:52:58.058:D / dalvikvm(774):GC_CONCURRENT释放<1K,3%免费 9191K / 9424K,暂停82ms + 6ms,总计125ms
08-12 08:52:58.058:D / dalvikvm(774):WAIT_FOR_CONCURRENT_GC已被屏蔽 40ms的
08-12 08:52:58.089:I / dalvikvm-heap(774):将堆(frag case)增长到 31.0744字节分配12.090MB
08-12 08:52:58.218:D / dalvikvm(774):GC_FOR_ALLOC释放<1K,2%免费 12263K / 12500K,暂停125ms,总计126ms
08-12 08:52:58.288:D / dalvikvm(774):GC_CONCURRENT释放0K,2%免费 12263K / 12500K,暂停4ms + 16ms,总计70ms
08-12 08:56:37.707:E / Trace(886):错误打开跟踪文件:没有这样的 文件或目录(2)08-12 08:56:38.518:D / dalvikvm(886): GC_CONCURRENT释放41K,6%免费2805K / 2968K,暂停13ms + 3ms,总计 77ms
08-12 08:56:38.518:D / dalvikvm(886):WAIT_FOR_CONCURRENT_GC已被屏蔽 10ms的
08-12 08:56:38.727:D / gralloc_goldfish(886):没有GPU的仿真器 检测到仿真。
08-12 08:56:55.107:D / dalvikvm(886):GC_FOR_ALLOC释放23K,5%免费 3083K / 3236K,暂停69ms,总计78ms
08-12 08:56:55.207:I / dalvikvm-heap(886):将堆(frag case)增长到 6.126MB用于3145744字节分配
08-12 08:56:55.367:D / dalvikvm(886):GC_FOR_ALLOC释放1K,3%免费 6153K / 6312K,暂停156ms,总计156ms
08-12 08:56:55.607:D / dalvikvm(886):GC_CONCURRENT释放4K,3%免费 6149K / 6312K,暂停80ms + 32ms,总计245ms
08-12 08:56:56.747:I / Choreographer(886):跳过465帧!该 应用程序可能在其主线程上做了太多工作。
08-12 08:58:03.287:D / dalvikvm(936):GC_CONCURRENT获得44K,6%免费 2805K / 2972K,暂停15ms + 3ms,总计76ms
08-12 08:58:03.287:D / dalvikvm(936):WAIT_FOR_CONCURRENT_GC已被屏蔽 9ms的
08-12 08:58:03.637:D / gralloc_goldfish(936):没有GPU的仿真器 检测到仿真。
答案 0 :(得分:0)
可能因为您要转换的位图是零字节:
//convert the image view into bitmap
iv.buildDrawingCache();
bmap = iv.getDrawingCache();
试试这个:
BitmapDrawable bd = (BitmapDrawable) iv.getDrawable();
bmap = bd.getBitmap();
希望这有帮助。