Android Question image processing

zmrcic

Member
Licensed User
how can I convert image to int() array? found to byte(), but not to int
I want to get black-white int() from image. image should be first converted to base64.
this is java example....pixel by pixel

canvas.toDataURL('image/jpeg').replace(/^data:image\/(png|jpg|jpeg);base64,/, "");

Java:
private static void convertARGBToGrayscale(int[] argb, int width, int height) {
        int pixels = width * height;

        for(int i = 0; i < pixels; ++i) {
            int r = argb[i] >> 16 & 255;
            int g = argb[i] >> 8 & 255;
            int b = argb[i] & 255;
            int color = r * 19 + g * 38 + b * 7 >> 6 & 255;
            argb[i] = color;
        }

    }
thx
 

Sandman

Expert
Licensed User
Longtime User
I'd recommend adding what you have in the beginning, and what your desired end result is. Perhaps there's an easier route.
 
Upvote 0

zmrcic

Member
Licensed User
thx klaus and Sandman...I think it is casting problem....
colors are from 16777215(white) to 0(black), so if I iterate over pixels I can get int of each pixel, like so
B4X:
            Dim color As Int = BM.GetPixel(x, y)
            Dim res(4) As Int
            res(0) = Bit.UnsignedShiftRight(Bit.And(color, 0xff000000), 24)
            res(1) = Bit.UnsignedShiftRight(Bit.And(color, 0xff0000), 16)
            res(2) = Bit.UnsignedShiftRight(Bit.And(color, 0xff00), 8)
            res(3) = Bit.And(color, 0xff)
I think Erel wrote this, it should be equivalent to
Java:
             int r = argb[i] >> 16 & 255;
            int g = argb[i] >> 8 & 255;
            int b = argb[i] & 255;
but
int color = r * 19 + g * 38 + b * 7 >> 6 & 255
give me cast type error, tried many things like
Dim color As Byte =Bit.ShiftRight(r * 19 + g * 38 + b * 7 , 6 ) And 255
but I need int here, not byte
Sandman, this code is part of old(new for me :) ) problem for printing image on BT printer. My printer(declared as ESC/POS?!) does not print images with libraries I found on B4x, so I have to roll my own. Can publish complete code if you think it will help. At the moment I can only print black box(filling that int() with 0), but I have to read it from locally stored image. So my code is OK, it can print, arrays and commands are OK, but image conversion is not.
thx again
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that BitmapCreatorEffects includes this feature.

Don't use Bitmap.GetPixel unless the image is very very small. Use BitmapCreator.
 
Upvote 0
Top