Search code examples
androidimagebitmapgetpixel

Android finding x and y positions with bitmap


I am having a some trouble figuring out my error. I am trying to find an x and y position of a bitmap based on the colors contained inside it. In this case I am using a blue color as a marker that has already been added to the background and finding what position it is at. Hear is my code:

`

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.util.Log;


public class drawing extends ImageView {

    Canvas mainCanvas;
    Drawable background;

public drawing(Context context){
        super(context);
 }
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        //creating a bitmap for editing with static image
        BitmapFactory.Options mNoScale = new BitmapFactory.Options();
        mNoScale.inScaled = false;
        Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.bmvfinal1, mNoScale);

        //calling method to find the text area and sending the bit map 
        findTextAreas(background);



    }

    private void findTextAreas(Bitmap myBitmap) {
         //setting the array for getting pixel values
         int[] colorArray;
        colorArray = new int[myBitmap.getHeight() * myBitmap.getWidth()];
        //getting the pixels from the bitmap
        myBitmap.getPixels(colorArray,0,myBitmap.getWidth(),0,0,myBitmap.getWidth(),myBitmap.getHeight() );


        //looping through the array of pixels
        for(int i = 0;i<colorArray.length;i++){
           int test =  colorArray[i];
        String testing = String.format("#%06X", (0xFFFFFF & test));
           //getting the values of the colors of each position in the array
       int blue = Color.blue(test);
       int red = Color.red(test);
       int green = Color.green(test);
         //finding the small dot that I added to the black and white page
               if (blue>200 && red<100 && green<10){
          Log.d("sdf","working!!!!!!!!!!!!!!!! "+ Integer.toString(i) );


               //trying to find the x and the y position with these calculation 
           int y = i/myBitmap.getWidth();
           int x = i-((y-1)*myBitmap.getWidth());
           Log.d("sdf","x: "+ Integer.toString(x)+ "y: " + Integer.toString(y) );
 }
}
        Log.d("sdf","done checking");
}

}

`

The code seems to be working fine until I try to find the x and the y values. It would seem that these are the proper equations but the result is the y in the correct spot but the x is to large. Any help or comments about code would be greatly appreciated.

Here is the image I am using: Link to image example


Solution

  • Try changing

    int x = i-((y-1)*myBitmap.getWidth());
    

    to

    int x = i%myBitmap.getWidth();
    

    The % is the modulo operator, which gets the remainder in an integer division. By taking the modulo of the bitmap's width, we can get the column number. For example, say our bitmap is 4x4 pixels:

    +-------------+
    | 0  1  2  3  |
    +-------------+
    | 4  5  6  7  |
    +-------------+
    | 8  9  10 11 |
    +-------------+
    | 12 13 14 15 |
    +-------------+
    

    Now, let's say that our pixel is number 5, if we do the following calculation:

    5 mod 4
    

    the result would be 1, since the integer division for 5/4 has a remainder of 1. Now, compare this to the table and you can see that the column number is also 1.

    Hope this helps!