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);
Related
In my application. There's section select block of apartment. In there a A and B block. So i want to choose A or B block from single image. Image is
There is what way to do this?
I guess you could use getLocationOnScreen()and hardcode the values you want. If user clicks on certain part of the image it prompts one response, if they click somewhere else it prompts another response. More info here Android: How do I get the x y coordinates within an image / ImageView?
Use BitMap concept. Convert full image as bitmap the crop the bitmap based on (x,y) positions.
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.myimage);
Bitmap cropbitmap=Bitmap.createBitmap(bitmap, xpoint,ypoint,width, height);
//default android method createBitmap(Bitmap source, int x, int y, int width, int height)
other simple way use library like this from github
android-crop
uCrop
Android-Image-Cropper
let's say i have this bitmap, which is a random shape painted all black, and say i want to be bale to change it color, does my bitmap have to be all painted white first or is there something else to it?
If you're using Canvas the way to alter the bitmap's color is to alter the bitmap itself. The steps involved are as follows:
Say you want to load an existing Bitmap you have somewhere and you want to tint it red somehow.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap);
After that you want to modify the bitmap's pixels before you paint it onto the Canvas. You create an int array that will holds all your pixels.
int[] pixels;
bitmap.getPixels(pixels, 0, 0, 0, 0, width, height);
After that you need to modify the array (say, adding to the red component). However, right now all we have is int values inside a pixel array. R,G and B are all packed inside. How to retrieve them?
int red = Color.red(pixels[n]);
int green = Color.green(pixels[n]);
int blue= Color.blue(pixels[n]);
Then you modify the pixel's value by whatever you want, you could put it in a loop or however you like, and then put it back to the pixels array. Also, RGB values go from 0-255 because they are 8-bit values.
Right after that you would put them back using exactly the opposite function.
bitmap.setPixels(pixels, 0, 0, 0, 0, width, height);
And then you're ready to go calling Canvas.drawBitmap();
Keep in mind that this process ought to be slow if you do it frequently, besides Canvas is a slow way of doing thing's if you're interested in real-time apps such as games.
Hope it helped!
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.
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.
I have an image file (.jpg) which contains image like Face. From my application i want to capture that particular Face part and copy that part into a new bitmap file. I have a rectangular co-ordinates of that Face part, so how do i capture only Face part from the image file and copy that into a bitmap file??
Could any body help me to get rid of this problem....
Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) will do the creation. For save, read this: Save bitmap to location
You could use
bitmap1.getPixel(x,y)
on the source image and using
bitmap2.setPixel(x,y,color)
on the destination image.
There is also a respective
bitmap1.getPixels( pixelArray, offset, stride, x, y, width, height)
which is undoubtedly faster.