I'm trying to build simple android project to convert image to binary format , but when I check pixels value ,i get zeros only. I don't know where is the problem. Any one can help me please. This is my code
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;
}
this is logcat
08-12 08:52:32.887: D/dalvikvm(774): GC_CONCURRENT freed 71K, 7% free 2765K/2964K, paused 23ms+15ms, total 94ms
08-12 08:52:33.497: D/gralloc_goldfish(774): Emulator without GPU emulation detected.
08-12 08:52:40.698: D/dalvikvm(774): GC_CONCURRENT freed 63K, 6% free 3087K/3272K, paused 73ms+3ms, total 165ms
08-12 08:52:49.397: I/Choreographer(774): Skipped 39 frames! The application may be doing too much work on its main thread.
08-12 08:52:54.568: D/dalvikvm(774): GC_FOR_ALLOC freed 9K, 5% free 3124K/3272K, paused 68ms, total 81ms
08-12 08:52:54.617: I/dalvikvm-heap(774): Grow heap (frag case) to 6.166MB for 3145744-byte allocation
08-12 08:52:54.778: D/dalvikvm(774): GC_FOR_ALLOC freed 40K, 4% free 6155K/6348K, paused 156ms, total 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): Skipped 412 frames! The application may be doing too much work on its main thread.
08-12 08:52:57.898: D/dalvikvm(774): GC_FOR_ALLOC freed 38K, 4% free 6119K/6348K, paused 28ms, total 30ms
08-12 08:52:57.929: I/dalvikvm-heap(774): Grow heap (frag case) to 9.091MB for 3145744-byte allocation
08-12 08:52:58.058: D/dalvikvm(774): GC_CONCURRENT freed <1K, 3% free 9191K/9424K, paused 82ms+6ms, total 125ms
08-12 08:52:58.058: D/dalvikvm(774): WAIT_FOR_CONCURRENT_GC blocked 40ms
08-12 08:52:58.089: I/dalvikvm-heap(774): Grow heap (frag case) to 12.090MB for 3145744-byte allocation
08-12 08:52:58.218: D/dalvikvm(774): GC_FOR_ALLOC freed <1K, 2% free 12263K/12500K, paused 125ms, total 126ms
08-12 08:52:58.288: D/dalvikvm(774): GC_CONCURRENT freed 0K, 2% free 12263K/12500K, paused 4ms+16ms, total 70ms
08-12 08:56:37.707: E/Trace(886): error opening trace file: No such file or directory (2) 08-12 08:56:38.518: D/dalvikvm(886): GC_CONCURRENT freed 41K, 6% free 2805K/2968K, paused 13ms+3ms, total 77ms
08-12 08:56:38.518: D/dalvikvm(886): WAIT_FOR_CONCURRENT_GC blocked 10ms
08-12 08:56:38.727: D/gralloc_goldfish(886): Emulator without GPU emulation detected.
08-12 08:56:55.107: D/dalvikvm(886): GC_FOR_ALLOC freed 23K, 5% free 3083K/3236K, paused 69ms, total 78ms
08-12 08:56:55.207: I/dalvikvm-heap(886): Grow heap (frag case) to 6.126MB for 3145744-byte allocation
08-12 08:56:55.367: D/dalvikvm(886): GC_FOR_ALLOC freed 1K, 3% free 6153K/6312K, paused 156ms, total 156ms
08-12 08:56:55.607: D/dalvikvm(886): GC_CONCURRENT freed 4K, 3% free 6149K/6312K, paused 80ms+32ms, total 245ms
08-12 08:56:56.747: I/Choreographer(886): Skipped 465 frames! The application may be doing too much work on its main thread.
08-12 08:58:03.287: D/dalvikvm(936): GC_CONCURRENT freed 44K, 6% free 2805K/2972K, paused 15ms+3ms, total 76ms
08-12 08:58:03.287: D/dalvikvm(936): WAIT_FOR_CONCURRENT_GC blocked 9ms
08-12 08:58:03.637: D/gralloc_goldfish(936): Emulator without GPU emulation detected.