We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi. I've been making an image mosaic collage android application using processing 2.0.3.
I made this application available with CASE 1 code and get the image above. Though, as you can see below, this code loops unusefully because of the structure.(I calculate all color of image in draw() because of file and folder selections. Maybe I did use wrong codes.). So I'm trying to fix this code not to loop unusefully. So I fix the code like CASE 2 but it doesn't work because of NullPointerException. I found a problem that some calculated arrays(myArrayR, myArrayG, myArrayB..) in flagA sentence did not send their values to flagB sentence. I've tried moving color calculating part into -->void folderselected()... but it didn't work either. How can I make my code like CASE 2 or is there any way to avoid unusefully loop of flagA sentence? Help me please.. I'm begging you. I've been trying to fix this problem for weeks... I'm leaving images that describe my CASE 1 and CASE 2 codes.Thank you for read!! :) CASE 1 :Wokrs well but loop unusefully
CASE 2 :NullPointerException error in flagB part
Whole code of CASE 1
//for filefilter
import java.io.File;
import java.io.FilenameFilter;
//for GUI controller(icons)
import controlP5.*;
ControlP5 cp5;
//for searching filess and folders
import select.files.*;
public boolean accept;
//select the source image folder and a basic image
SelectLibrary folders;
int flagA = 4;
String folderadr;
SelectLibrary files;
int flagB = 0;
int flagD = 0;
PImage originalImage;
//setting the size of grids, transparency and accuracy
int gridnum;
int gridsize = 15;
int tran = 255;
int brightness = 255;
int quality = 2;
//finding average colors of original image
int[] mini;
int[] AverageR;
int[] AverageG;
int[] AverageB;
int loc = 0;
//to pick up only image(jpg, png, gif..) files
java.io.File folder;
java.io.FilenameFilter jpgFilter;
//to get image files' names to use loadimage() for analysing
String[] filenames;
PImage[] myImage;
int[] myArrayR;
int[] myArrayG;
int[] myArrayB;
//to find the colosest image in color to the original image
int[][] delta;
void setup(){
size(displayWidth, displayHeight);
//for file select
files = new SelectLibrary(this);
files.selectInput("Select a file to process:", "fileSelected");
//for folder select
folders = new SelectLibrary(this);
folders.selectFolder("Select a folder to process:", "folderSelected");
//for placing several icons
int lll = floor(displayWidth/20);
cp5 = new ControlP5(this);
//grid 7 button
cp5.addButton("buttonA")
.setPosition(lll,displayHeight-48)
.setImages(loadImage("1.gif"), loadImage("1.gif"), loadImage("6.gif"))
.updateSize();
//grid 10 button
cp5.addButton("buttonB")
.setPosition(lll*4,displayHeight-48)
.setImages(loadImage("2.gif"), loadImage("2.gif"), loadImage("6.gif"))
.updateSize();
//process button
cp5.addButton("buttonC")
.setPosition(lll*7,displayHeight-48)
.setImages(loadImage("3.gif"), loadImage("3.gif"), loadImage("6.gif"))
.updateSize();
//high acuracy button
cp5.addButton("buttonD")
.setPosition(lll*14,displayHeight-48)
.setImages(loadImage("4.gif"), loadImage("4.gif"), loadImage("6.gif"))
.updateSize();
//low accuracy button
cp5.addButton("buttonE")
.setPosition(lll*17,displayHeight-48)
.setImages(loadImage("5.gif"), loadImage("5.gif"), loadImage("6.gif"))
.updateSize();
}
//grid 7 button
public void buttonA(int theValue) {
println("a button event from buttonA: "+theValue);
gridsize = 7;
}
//grid 10 button
public void buttonB(int theValue) {
println("a button event from buttonB: "+theValue);
//flagD = 1;
gridsize = 10;
}
//process button
public void buttonC(int theValue) {
println("a button event from buttonC: "+theValue);
//quality = 3;
flagD = 1;
}
//high acuracy button
public void buttonD(int theValue) {
println("a button event from buttonD: "+theValue);
quality = 1;
}
//low accuracy button
public void buttonE(int theValue) {
println("a button event from buttonE: "+theValue);
quality = 3;
}
//select the source folder and a basic image
void fileSelected(File selection) {
if (selection == null) {
println("Nothing was selected.");
} else {
println("User selected " + selection.getAbsolutePath());
originalImage = loadImage(selection.getAbsolutePath());
flagB = 1;
}
}
void folderSelected(File selection) {
if (selection == null) {
println("Nothing was selected.");
} else {
println("User selected " + selection.getAbsolutePath());
folderadr = (selection.getAbsolutePath());
println(folderadr);
flagA = 1;
}
}
void draw(){
//run after the folder selection - flagA
if (flagA == 1){
java.io.File folder = new java.io.File(dataPath(folderadr));
java.io.FilenameFilter jpgFilter = new java.io.FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".png");
}
};
String[] filenames = folder.list(jpgFilter);
PImage[] myImage = new PImage[filenames.length];
int[] myArrayR = new int[myImage.length];
int[] myArrayG = new int[myImage.length];
int[] myArrayB = new int[myImage.length];
//find average RGB color values of multiple images
for (int a = 0; a < filenames.length; a++){
myImage[a] = loadImage(folderadr+"//" + filenames[a]);
myImage[a].loadPixels();
int sr = 0;
int sg = 0;
int sb = 0;
for (int x = 0; x < (myImage[a].width)*(myImage[a].height); x = x + quality){
color b = myImage[a].pixels[x]; // get color values without displaying
int br = int(red(b));
int bg = int(green(b));
int bb = int(blue(b));
sr = sr + br;
sg = sg + bg;
sb = sb + bb;
}
myArrayR[a] = sr/((myImage[a].width)*(myImage[a].height)/quality);
myArrayG[a] = sg/((myImage[a].width)*(myImage[a].height)/quality);
myArrayB[a] = sb/((myImage[a].width)*(myImage[a].height)/quality);
}
//run after the file(originalImage) selection - flagB
if (flagB == 1){
image(originalImage,0,0);
for (int a = 0; a < filenames.length; a++){
println("flagB = "+ flagB + " myArrayR,G,B =" + myArrayR[a]+","+myArrayG[a]+","+myArrayB[a]);
}
//finding average colors of original image
gridnum = (ceil((originalImage.width)/gridsize))*(ceil((originalImage.height)/gridsize));
int[] mini = new int[gridnum];
int[] AverageR = new int[gridnum];
int[] AverageG = new int[gridnum];
int[] AverageB = new int[gridnum];
int[][] delta = new int[gridnum][myImage.length];
//run when the "process" button was pushed
if (flagD == 1){
int m = ceil(480/gridsize);
originalImage.loadPixels();
//start analysing the "k" th grid
for (int k = 0; k < (ceil((originalImage.width)/gridsize))*(ceil((originalImage.height)/gridsize)); k++){
int h = gridsize*floor(k/m);
int w = gridsize*(k - (h/gridsize)*(ceil(originalImage.width/gridsize)));
// get average colors of each area of original image
int SumOfR = 0;
int SumOfG = 0;
int SumOfB = 0;
int AverR = 0;
int AverG = 0;
int AverB = 0;
int l = w+(h*originalImage.width);
for (int a = 0; a < gridsize; a++){
for (int c = 0; c < gridsize; c++){
color myPixel = originalImage.pixels[l+a+(originalImage.width*c)];
int R = int (red(myPixel));
int G = int (green(myPixel));
int B = int (blue(myPixel));
SumOfR = SumOfR + R;
SumOfG = SumOfG + G;
SumOfB = SumOfB + B;
}
}
AverR = SumOfR / (gridsize * gridsize);
AverG = SumOfG / (gridsize * gridsize);
AverB = SumOfB / (gridsize * gridsize);
AverageR[k] = AverR;
AverageG[k] = AverG;
AverageB[k] = AverB;
for(int i = 0; i < myImage.length; i++){
delta[k][i] = (AverageR[k]-myArrayR[i])*(AverageR[k]-myArrayR[i]) + (AverageG[k]-myArrayG[i])*(AverageG[k]-myArrayG[i]) + (AverageB[k]-myArrayB[i])*(AverageB[k]-myArrayB[i]);
}
mini[k] = min(delta[k]);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for(int j = 0; j < myImage.length; j++){
if(mini[k] == delta[k][j]){
//tint(brightness, tran);
image(myImage[j], w, h, gridsize, gridsize);
}
}
flagA = 7;
} //end analysing the "k" th grid
} //if flagD == 1
} //if flagB == 1
} //if flagA == 1
} //void draw()
:D
Answers
When I see a line like
int[] AverageR
in two different places in the code, I am suspecting an error... The second declaration hides the first one and this definition disappears as soon as a } is meet.See also the article Why do I get a NullPointerException?.
Hi PhiLho! Thank you for answering! But the part of int []AverageR is okay.. the part int []myArrayR part has NPE error. I think I declared arrays only once like int []myArrayR; and int []myArrayR = new int[gridnum]; .What do I have to do? Thank you always.