ankita gahoi
ankita gahoi

Reputation: 1562

how to get pixel Color value on a zoomed bitmap?

I am using zooming and panning with sony ericssion tutorial with this link

http://blogs.sonyericsson.com/developerworld/2010/06/09/android-one-finger-zoom-tutorial-part-3/

and when I am using Bitmap.getPixel(x,y) in ontouchevent of image.Its giving different different RGB values of color.After zooming bitmap same problem occurs.I have tried x y coordinates diviing by zooming factor also.it doesnt work for me.

can plz someone help me to solve this problem.how to get the color value on a zoomed Bitmap?

Upvotes: 2

Views: 2044

Answers (2)

dharmendra
dharmendra

Reputation: 7881

Bitmap.getPixel(x,y) get damm slow result . prefer

int [] byteArry= new int [Bitmap.getWidth()*Bitmap.getHeight()] ;

Bitmap.getPixels(byteArry, 0, Bitmap.getWidth(), 0, 0, slotBitmap_1.getWidth(), Bitmap.getHeight());

the byteArry will returns the pixels color of image.

Upvotes: 0

pcans
pcans

Reputation: 7651

getPixel is working on your unzoomed Bitmap. I assume X and Y come from a touch event and are relative to the View. I see in your link that the example is doing the resize using two Rect objects.

@Override
protected void onDraw(Canvas canvas) {
    if (mBitmap != null && mState != null) {
        canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint);
    }
}

I assume that is is also how you did it. Simply dividing your coordinates with the zoom ratio will not work because X and Y are relative to your view and not to the RectDst.

I generally use Matrix objects to do zooming because it is so easy to reproduce, invert, etc... For exemple if your zoom had been done using Matrix you'll be able to invert it, give it your X,Y and get your original coordinates relative to your bitmap back.

But you've got a way to do it anyway, by calculating the Matrix using the 2 Rects:

//Recreate the Matrix using the 2 Rects
Matrix m = new Matrix()
m.setRectToRect(mRectDst, mRectSrc, false);
//Transform the coordinates back to the original plan
float[] src = {x,y};
float[] dst = new float[2];
m.mapPoints(src, dst);
myBitmap.getPixel(dst[0], dst[1]);

I did not test it but it should work or at least help you find your solution.

Upvotes: 1

Related Questions