i try creating a scaled bitmap using that method but once i access the bitmap, my app crashes with the following message:
"java.lang.IllegalArgumentException: y + height must be <= bitmap.height()"
When I debug it, I can see that immediately after calling "createScaledBitmap", the resulting bitmap has a size of -1, -1. if i use "createBitmap" instead, the resulting bitmap does show the size of the original bitmap.
Should nobody here know what the cause of this is... is there maybe a workaround to resize a bitmap differently? I couldn't find anything in the bitmap class but maybe somebody here knows another way?
First, i want to clarify that you are getting an error from Bitmap.createBitmap method, not from Bitmap.createScaledBitmap. Syntax of createBitmap is as shown below,
Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
Parameters
source: The bitmap we are subsetting
x: The x coordinate of the first pixel in source
y: The y coordinate of the first pixel in source
width: The number of pixels in each row
height: The number of rows
m: Optional matrix to be applied to the pixels
filter: true if the source should be filtered. Only applies if the matrix contains more than just translation.
An error is occured because,
x, y, width, height values are outside of the dimensions of the source bitmap.
Related
Using MikeOrtiz's awesome ImageView implementation with touch and zoom events, I wanted to crop a picture taken with the camera to match the zoom. Using his method...
// Return a Rect representing the zoomed image.
RectF getZoomedRect();
...I tried cropping the resulting picture bitmap to the zoom size like so:
RectF zoomCoordinates = mTouchImageView.getZoomedRect();
Bitmap croppedBitmapToOverview = Bitmap.createBitmap(
AppResources.sCurrentImage,
((int) zoomCoordinates.left),
((int) zoomCoordinates.top),
((int) zoomCoordinates.width()),
((int) zoomCoordinates.height()));
However I get a "must be bigger than 0" error with this. While debugging I noticed ALL values were 0 due to casting to an Integer. The real values however go something like this:
//Log.d print for each of those fields without the int cast
Left 0.34047672
Top 0.20797288
Width 0.33333334
Height 0.3429547
So there's my problem, but I can't see how to fix this. I've never worked with bitmaps before or canvas, Rect, etc.
Is there some tweaking I could do to these values, or should I take a different approach altogether?
Got around the problem by simply taking a "screenshot" of the View of sorts. This got me a Bitmap with the picture as it was zoomed
mTouchImageView.setDrawingCacheEnabled(true);
AppResources.sCurrentImage = Bitmap.createBitmap(mTouchImageView.getDrawingCache());
multiply the coordinates with the size of your image like
(int)(zoomCoordinates.left * imageSize)
Is the Android documentation for canvas.drawBitmap wrong? It says:
public void drawBitmap (Bitmap bitmap, float left, float top, Paint paint)
Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix.
Well, x and y don’t seem to be floats, they’re ints; is that correct?
Say I want to overlay the bitmap (which is the size of the available screen, and is bound to a canvas of the same) over the whole available screen. It seems sensible I would:
canvas.drawBitmap(myBitmap, 0, 0, mPaint);
doesn’t it?
But that doesn’t work. What does seem to work is:
canvas.drawBitmap(myBitmap, 2000000, 1000000, mPaint).
Now that statement seems to me to tell the bitmap that it should draw itself a huge distance
Outside the screen! What am I missing here?
In this method x and y are floats, not ints. But like mentioned in the documentation, the x and y coordinates of the bitmaps will be affected by the matrix currently set on the Canvas. In the case of a ScrollView for instance, the matrix could very well contain a very large translation.
What this means is that the coordinates 0, 0 will draw the bitmap at the current origin of the Canvas. That origin is defined by the matrix you can query with getMatrix().
I used this function in my Android program:
public void drawBitmap (Bitmap bitmap, float left, float top, Paint paint)
However, I want to draw my bitmap not in the position 0 x 0, but in the position 10 x 10 (in PIXELS). The drawBitmap function, however, only accepts float numbers...
How can I achieve this??
Thank you in advance!
Have you tried drawBitmap(bitmap, 10.f, 10.f, ... )? Considering the transformation matrix of the canvas is set to the identity matrix, that is.
The reason those parameters are float is probably that the Canvas does not operate in an integer space (pixels), but in a user specified space defined by a transformation matrix. If you where to set a custom transformation matrix to scale by 2 then using 0.5, 0.5 would end up mapping to pixel 1, 1. This means you could also set a custom transformation to translate by 10, 10 and then just simply draw the bitmap without specifying a destination.
I need to develop an android application which do some image processing on the image input. For this, I need to get the pixel values of the bitmap in either an array or an object. Can anyone help me to grab the pixel-level details of a bitmap image.
Thanks !
to get pixel array of bitmap
bitmap.getPixels(pixels, offset, stride, x, y, width, height);
every pixel/value in array will be represented as Color with ARGB value.
if you want to get the value of on pixel do this
int pixel = bitmap.getPixel(x, y);
I want to know how to cut an image in jigsaw form in android at run time. What i actually want is user can enter any image and then made puzzle of it.
Any API or method for doing this in android ?
Thanks in Advance.
You can use this method in Bitmap to cut it into a new BitMap of specified width and height pixels
starting from specified pixel location of x and y
public static Bitmap createBitmap (Bitmap source,
int x,
int y, int width, int height)
Assuming your puzzle piece is square, you can use createBitmap() function.
Returns an immutable bitmap from the specified subset of the source
bitmap. The new bitmap may be the same object as source, or a copy may
have been made. It is initialized with the same density as the
original bitmap.