繁体   English   中英

Android使用位图查找x和y位置

[英]Android finding x and y positions with bitmap

我在弄清楚我的错误时遇到了一些麻烦。 我试图根据其中包含的颜色查找位图的x和y位置。 在这种情况下,我使用一种蓝色作为已经添加到背景中的标记,并查找它所处的位置。 听到是我的代码:

`

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");
}

}

`

在我尝试找到x和y值之前,代码似乎工作正常。 看来这些是正确的方程式,但结果是正确位置上的y,但x很大。 任何有关代码的帮助或评论将不胜感激。

这是我正在使用的图像: 链接到图像示例

尝试改变

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

int x = i%myBitmap.getWidth();

%是取模运算符,它以整数除法得到余数。 通过对位图的宽度取模,我们可以获得列号。 例如,假设我们的位图是4x4像素:

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

现在,假设我们进行以下计算,则像素为5:

5 mod 4

结果将为1,因为5/4的整数除法为1的余数。 现在,将其与表进行比较,您可以看到列号也是1。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM