0

In my app I am capturing images and on button press just apply some filter (function). SOLVED:

So the problem is when I convert bitmap to byteArray I will get RGBA byte format, so access to the RED is every first position of 4th byte. Thats simple, but what I dont understand is: why black is 0 not -128 and white is -1 not 127 //4 bytes: R G B A //4 bytes: 0 1 2 3 //byte => -128 to 127 //black -> grey -> greyer -> white // 0 -> 127 -> -128 -> -1
SO any ideas how to get number from 0 to 255 or -128 to 127? equils to black to white transition.

WAS SOLVED by adding value=value & 0xFF

this.actualPosition = 1; //Green
    for(int j=0; j < bmpHeight ; j++) {
        for(int i=0 ; i < bmpBytesPerPixel/4 ;i++) {

            byte midColor =  (byte) ( (byteArray[actualPosition-1]& 0xFF) * 0.30 + (byteArray[actualPosition]& 0xFF) * 0.59 + (byteArray[actualPosition+1]& 0xFF) * 0.11 );
            byteArray[actualPosition]=midColor;
            byteArray[actualPosition+1]=midColor;
            byteArray[actualPosition+-1]=midColor;

            //byteArray[actualPosition+1]=byteArray[actualPosition];
            //byteArray[actualPosition+-1]=byteArray[actualPosition];
            actualPosition += 4;
        }
    }

Trying to make the fastest algorithm. This one is about ~2.7s when working with HD image / bitmap, so the bytearray lenght is 4 * 1080 * 720 = 3 110 400 bytes. Accesing 3/4.

there is how I am converting bitmap to byteArray and contrariwise.

private void getArrayFromBitmap() {
        // Převod Bitmap -> biteArray
        int size = this.bmpBytesPerPixel * this.bmpHeight;
        ByteBuffer byteBuffer = ByteBuffer.allocate(size);
        this.bmp.copyPixelsToBuffer(byteBuffer);
        this.byteArray = byteBuffer.array();
    }

    private void getBitmapFromArray() {
        // Převod biteArray -> bitmap
        Bitmap.Config configBmp = Bitmap.Config.valueOf(this.bmp.getConfig().name());
        this.bmp = Bitmap.createBitmap(this.bmpWidth, this.bmpHeight, configBmp);
        ByteBuffer buffer = ByteBuffer.wrap(this.byteArray);
        this.bmp.copyPixelsFromBuffer(buffer);
        System.out.println("\n DONE "+ configBmp);
    }
Jan Retych
  • 53
  • 8
  • I'm not usually code in Java, but it looks like Java treated every byte as signed int. You need to convert it into unsigned int. You can check [this](https://stackoverflow.com/questions/7401550/how-to-convert-int-to-unsigned-byte-and-back/17586918) – Smankusors Dec 30 '18 at 18:17
  • All Java integral types are signed, so yes if you're expecting unsigned values you have to convert them. Use the `(0xFF & byte)` trick as @Smankusors showed in that link. – markspace Dec 30 '18 at 18:40
  • Ok, its seems value & 0xFF will solve my problem, so the last question is how to speed up algorithm – Jan Retych Dec 30 '18 at 18:42

0 Answers0