我正在尝试构建简单的 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;
}
这是日志猫
08-12 08:52:32.887: D/dalvikvm(774): GC_CONCURRENT freed 71K, 7% free 2765K/2964K, paused 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 freed 63K, 6% free 3087K/3272K, paused 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): 将堆(碎片情况)增加到 6.166MB,分配 3145744 字节
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 freed <1K, 4% free 6155K/6348K, paused 80ms+38ms, total 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): 将堆(碎片情况)增加到 9.091MB,分配 3145744 字节
08-12 08:52:58.058: D/dalvikvm(774): GC_CONCURRENT freed <1K, 3% free 9191K/9424K, paused 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): 将堆(碎片情况)增加到 12.090MB,分配 3145744 字节
08-12 08:52:58.218: D/dalvikvm(774): GC_FOR_ALLOC 释放 <1K, 2% free 12263K/12500K, 暂停 125ms, 总共 126ms
08-12 08:52:58.288: D/dalvikvm(774): GC_CONCURRENT freed 0K, 2% free 12263K/12500K, paused 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 % free 2805K/2968K,暂停13ms+3ms,共77ms
08-12 08:56:38.518: D/dalvikvm(886): WAIT_FOR_CONCURRENT_GC 阻塞 10 毫秒
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): 将堆(碎片情况)增加到 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 freed 4K, 3% free 6149K/6312K, paused 80ms+32ms,总共245ms
08-12 08:56:56.747: I/Choreographer(886): 跳过 465 帧!应用程序可能在其主线程上做了太多工作。
08-12 08:58:03.287: D/dalvikvm(936): GC_CONCURRENT freed 44K, 6% free 2805K/2972K, paused 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 仿真的仿真器。