This code gives me an Unhandled Exception error everytime I try to run in on a mobile phone (Nokia E51) although it works perfectly in an emulator. What seems to be the problem?
public class Video2 extends MIDlet implements CommandListener {
// Class to capture the snapshot
public class Video extends Thread {
SnapTimerTask st;
Image images1[];
int num;
RecordStore rs = null;
public Video(SnapTimerTask st) {
this.st = st;
}
public void run() {
captureVideo();
}
public void captureVideo() {
RecordStore imagesRS = null;
try {
form.deleteAll();
byte[] raw = videoControl.getSnapshot(null);
//display.setCurrent(form);
Image image = Image.createImage(raw, 0, raw.length);
int rgbArray[] = null;
int width = image.getWidth();
int height = image.getHeight();
int[] pixelData = new int[height*width];
int[][] rgbData = new int[height*width][3];
int[][] white_area = new int[2][3];
int[] white_area_pixelCount = new int[2];
/*resize image*/
image = resize(image, 160, 120, false, false);
/*get pixels of image*/
image.getRGB(pixelData, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
int j, c, r, g, b, a, i;
// 1. get original histogram
int[][] histogram = getARGBHistogram(image);
// 2. build the new component transformation tables
final int total = width * height;
byte [][] transform = new byte[4][];
for(c = 1; c < 4; c++) {
int sum = 0;
transform[c] = new byte[256];
for(j = 0; j < 255; j++) {
sum += histogram[c][j];
int l3 = (sum / 2 + sum * 256 ) / total;
transform[c][j] = (byte) l3;
}
}
// 3. apply the transformation to the original image
image = transformARGB(image, null, transform[1], transform[2], transform[3]);
int red = 0;
int green = 0;
int blue = 0;
int redctr = 0;
int greenctr = 0;
int bluectr = 0;
int[][] Image_RGB = new int[width][height];
int[][] Average_color = new int[3][width];
int[][] color_counter = new int[3][3];
form.append("width" + image.getWidth() + " height: " + image.getHeight() + "\n" );
//Storing data to Array
for(a =10; a< width*height; a++){
j = a/width;
i = a % width;
c = pixelData[a];
r = (c & 0x00ff0000) >> 16;
g = (c & 0x0000ff00) >> 8;
b = c & 0x000000ff;
Image_RGB[i][j] = pixelData[a];
}
for(i=102; i < 123; i++){
for(j=17; j < 118; j++){
c = Image_RGB[i][j];
r = (c & 0x00ff0000) >> 16;
g = (c & 0x0000ff00) >> 8;
b = c & 0x000000ff;
red = red + r;
green = green + g;
blue = blue + b;
}
red = red/101;
green = green/101;
blue = blue/101;
if(Math.max(red, Math.max(green,blue)) == red){
color_counter[0][0]++;
}
else if (Math.max(red, Math.max(green,blue)) == green){
color_counter[0][1]++;
}
else{
color_counter[0][2]++;
}
}
form.append("red: " + red + "green: " + green + "blue: " + blue + "\n");
redctr = greenctr = bluectr = 0;
for(i=140; i < 150; i++){
for(j=36; j < 84; j++){
c = Image_RGB[i][j];
r = (c & 0x00ff0000) >> 16;
g = (c & 0x0000ff00) >> 8;
b = c & 0x000000ff;
if(r > 200 ){
red = red + r;
green = green + g;
blue = blue + b;
}
}
red = red/48;
green = green/48;
blue = blue/48;
if(Math.max(red, Math.max(green,blue)) == red){
color_counter[1][0]++;
}
else if (Math.max(red, Math.max(green,blue)) == green){
color_counter[1][1]++;
}
else{
color_counter[1][2]++;
}
}
form.append("red: " + red + "green: " + green + "blue: " + blue + "\n");
redctr = greenctr = bluectr = 0;
for(i=120; i < 140; i++){
for(j=20; j < 68; j++){
c = Image_RGB[i][j];
r = (c & 0x00ff0000) >> 16;
g = (c & 0x0000ff00) >> 8;
b = c & 0x000000ff;
red = red + r;
green = green + g;
blue = blue + b;
}
red = red/48;
green = green/48;
blue = blue/48;
if(Math.max(red, Math.max(green,blue)) == red){
color_counter[2][0]++;
}
else if (Math.max(red, Math.max(green,blue)) == green){
color_counter[2][1]++;
}
else{
color_counter[2][2]++;
}
}
form.append("red: " + red + "green: " + green + "blue: " + blue + "\n");
if(((color_counter[0][1] | color_counter[0][2]) > color_counter[0][0]) & (((color_counter[0][2]|color_counter[0][1]) - color_counter[0][0]) > 2 )) form.append("20 peso bill\n");
else if(color_counter[2][2] > (color_counter[2][0] | color_counter[2][1])) form.append("50 peso bill\n");
else if(color_counter[1][0] > (color_counter[1][1] | color_counter[1][2])) form.append("100 peso bill\n");
else if(color_counter[1][2] > (color_counter[1][0] | color_counter[1][1])) form.append("1000 peso bill\n");
form.append(image);
display.setCurrent(form);
/*end get pixel of image*/
player.close();
player = null;
videoControl = null;
} catch (MediaException me) { }
}
};
// Task to initiate the task to take the snapshot
class SnapTimerTask extends TimerTask {
public final void run() {
video = new Video(this);
video.start();
}
};
private Display display;
private Form form;
private Command exit,back,camera,cancel;
private Player player;
private VideoControl videoControl;
private CameraControl cameraControl;
SnapshotControl snapshotControl;
private Video video;
private Timer timer;
private SnapTimerTask task;
public Video2() {
exit = new Command("Exit", Command.EXIT, 0);
camera = new Command("Camera", Command.SCREEN, 0);
back = new Command("Back", Command.BACK, 0);
cancel = new Command("Cancel", Command.STOP, 0);
form = new Form("Capture Video");
form.addCommand(camera);
form.setCommandListener(this);
}
public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s) {
if (c == exit) {
destroyApp(true);
notifyDestroyed();
}
else if (c == camera) {
showCamera();
}
else if (c == back) {
timer.cancel();
//display.setCurrent(form);
}
else if(c == cancel)
timer.cancel();
}
public void showCamera() {
try {
timer = new Timer();
task = new SnapTimerTask();
timer.schedule(task,5000); // 5000 ms
player = Manager.createPlayer("capture://video");
player.realize();
videoControl = (VideoControl)player.getControl("VideoControl");
Canvas canvas = new VideoCanvas1(this, videoControl);
canvas.addCommand(camera);
canvas.addCommand(exit);
canvas.addCommand(cancel);
canvas.setCommandListener(this);
display.setCurrent(canvas);
player.start();
}
catch (IOException ioe) {}
catch (MediaException me) {}
}