ArrayIndexOutOfBounds, can't figure why? - java

i wrote this method:
public static Bitmap matrixToBitmap(int[][] slika)
{
int w = slika[0].length;
int h = slika[1].length;
Bitmap into = Bitmap.createBitmap(w, h, Config.ARGB_8888);
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
if(slika[x][y] < 128)
into.setPixel(x, y, Color.BLACK);
else
into.setPixel(x, y, Color.WHITE);
}
}
return into;
}
and when I call it inside my android app with an int[454][454] array, it says this in Logcat:
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=452; index=452
pointing at this line of matrixToBitmap method:
if(slika[x][y] < 128)
Can someone figure why is it happening? Values for w and h become 454 and 454, just as they should be.

error is here:
int w = slika[0].length;
int h = slika[1].length;
what happened is, you set up length of first row from your array to be w, and length of your second row to be h
to make it work, change it to:
int w = slika.length;
int h = slika[0].length;

Related

how to Pixelize a Image with JavaFX?

I want to pixelize a Image with JavaFx.
My problem is that I only have one written pixel in the end, so that it works for just one time.
i tried a
Here is my code:
Image img = imgView.getImage();
PixelReader pixelReader = img.getPixelReader();
WritableImage wImage = new WritableImage(
(int) img.getWidth(),
(int) img.getHeight());
PixelWriter pixelWriter = wImage.getPixelWriter();
for (int y = 1; y < img.getHeight(); y += 3) {
for (int x = 1; x < img.getWidth(); x += 3) {
Color px = pixelReader.getColor(x, y);
float red = (float) px.getRed();
float green = (float) px.getGreen();
float blue = (float) px.getBlue();
Color all = new Color(red / 3, green / 3, blue / 3, 1);
for (int u = 0; u <= 3; u++) {
for (int i = 0; i <= 3; i++) {
pixelWriter.setColor(u, i, all);
}
}
}
}
Just check the part where you set the color:
for (int u = 0; u <= 3; u++) {
for (int i = 0; i <= 3; i++) {
pixelWriter.setColor(u, i, all);
}
}
As you can see you always set the color of pixel at (0,0) - (3,3).
You need to use
pixelWriter.setColor(x + u, y + i, all);
However, you need to be sure that you won't try to set color of some pixels outside the image. Check the boundaries of loops by x, y, u and i.

Edit 2D array instance variable size without Array List (image manipulation)

I need to edit the original instance variable private in[][] pixels; to be twice the width, I've done the algorithm to mirror the image and make a new array twice as wide I just don't know how to set the original int[][] pixels to it. The pixels array has to be the one modified, it can't go by another name.
private int[][] pixels;
...
if(transformationName == "Mirror"){
int[][] mirrorTemp = new int[height][width*2];
for(int h = 0; h < height; h++){
for(int w = 0; w < width; w++){
mirrorTemp[h][w] = pixels[h][w];
mirrorTemp[h][w + width] = pixels[h][width - h - 1];
}
}
int[][] pixels = new int[height][width*2];
for(int h = 0; h < height; h++){
for(int w = 0; w < (width*2); w++){
pixels[h][w] = mirrorTemp[h][w];
}
}
}
First, I believe the following is what you intended to do.
for(int h = 0; h < height; h++){
for(int w = 0; w < width; w++){
mirrorTemp[h][w] = pixels[h][w];
mirrorTemp[h][w + width] = pixels[h][width - w];
}
}
After this, you can simply
pixels = mirrorTemp;
since the two are both int[][] type.
Hope this helps.

How to get average RGB value of Bitmap on Android?

I know how to get the RGB values of individual pixels of a bitmap. How can I get the average RGB value for all of the pixels of a bitmap?
I think below code for exact answer to you.
Get the Average(Number of pixels)of Red, Green and Blue value for the given bitmap.
Bitmap bitmap = someBitmap; //assign your bitmap here
int redColors = 0;
int greenColors = 0;
int blueColors = 0;
int pixelCount = 0;
for (int y = 0; y < bitmap.getHeight(); y++)
{
for (int x = 0; x < bitmap.getWidth(); x++)
{
int c = bitmap.getPixel(x, y);
pixelCount++;
redColors += Color.red(c);
greenColors += Color.green(c);
blueColors += Color.blue(c);
}
}
// calculate average of bitmap r,g,b values
int red = (redColors/pixelCount);
int green = (greenColors/pixelCount);
int blue = (blueColors/pixelCount);
The answer from john sakthi does not work correctly if the Bitmap has transparency (PNGs). I modified the answer for correctly getting the red/green/blue averages while accounting for transparent pixels:
/**
* Calculate the average red, green, blue color values of a bitmap
*
* #param bitmap
* a {#link Bitmap}
* #return
*/
public static int[] getAverageColorRGB(Bitmap bitmap) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
int size = width * height;
int pixelColor;
int r, g, b;
r = g = b = 0;
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
pixelColor = bitmap.getPixel(x, y);
if (pixelColor == 0) {
size--;
continue;
}
r += Color.red(pixelColor);
g += Color.green(pixelColor);
b += Color.blue(pixelColor);
}
}
r /= size;
g /= size;
b /= size;
return new int[] {
r, g, b
};
}
you can use this method for this purpose: Bitmap.createBitmap
For instance:
int[] colors = new int[yourWidth * yourHeight];
Arrays.fill(colors, Color.Black);
Bitmap bitamp = Bitamp.createBitmap(colors, yourWidth, yourHeight, Bitmap.Config.ARGB_8888);
Check for typo

Why is my Android app so slow?

I have an image (of size 1024x1024) in an int array (int[] pixels) and I am inverting one channel using the following loop...
int i = 0;
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
int color = pixels[i];
pixels[i] = Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color));
i++;
}
}
This takes more than 1 second on my new Galaxy S4 phone. Similar loop runs in a blink of an eye even on an older iPhone. Is there something I am doing wrong here?
If I replace "Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color))" with "Color.BLUE", it gets much faster.
Found a workaround.
If I use my own bitwise operators instead of Color functions, it gets much faster...
int i = 0;
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
int color = pixels[i];
int red = ((color & 0x00ff0000) >> 16);
pixels[i] = (color & 0xff00ffff) | ((255 - red) << 16);
//pixels[i] = Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color));
i++;
}
}
You may want to consider using the ColorMatrix class to get this done: http://developer.android.com/reference/android/graphics/ColorMatrix.html
There is most likely considerable overhead involved when manipulating individual pixels the way you are doing it. The ColorMatrix class is your friend.
I think if you replace this piece of code by this, it will be relatively fast
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int pixels[] = new int[w * h];
bitmap.getPixels(pixels, 0, w, 0, 0, w, h);
int n = w * h;
for (int i = 0; i < n; i++) {
int color = pixels[i];
pixels[i] = Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color));
}
bitmap.setPixels(pixels, 0, w, 0, 0, w, h);

Difference between direct putting 2D array values in image and making a pixel from these values and then putting it

I am a beginner in digital image processing field. right now i am coding to get an image from 2D array... i have downloaded these two code samples... when i am using the first one i am getting correct image, but when i am using the second one i am getting processed image with more brightness as compared to the previous one.
Please suggest me what is the difference between these codes and also which code should i refer... i have to use this code to get my research papers results...
1).
public BufferedImage getImage(BufferedImage original, float paddedArray[][], int width, int height) {
BufferedImage img = new BufferedImage(width, height, original.getType());
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int c = Math.round(paddedArray[i][j]);
int pixel = c << 16 | c << 8 | c;
img.setRGB(i, j, pixel);
}
}
return img;
}
2).
public BufferedImage getImages(BufferedImage original, float paddedArray[][], int width, int height) {
BufferedImage img = new BufferedImage(width, height, original.getType());
Graphics2D g = (Graphics2D) img.getGraphics();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int c = Math.round(paddedArray[i][j]);
if (c >= greyRange) {
c = greyRange - 1;
}
g.setColor(new Color(c, c, c));
g.fillRect(i, j, 1, 1);
}
}
return img;
}
these codes i am using i only want to know the difference between
g.setColor(new Color(c, c, c)); and img.setRGB(i, j, pixel);

Categories

Resources