Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
gbe
Contributor
gbe
Posts: 22
Registered: ‎08-14-2009
My Device: BlackBerry Pearl
Accepted Solution

Taking picture with camera...

 

Dear all, 

 

I'm currently facing lots of problems to use the camera. After days and days of trying to change/adapt my code, you are my last hope... :Tt2:

 

What i want to do is  :

 

  1. Starting camera
  2. Taking a snapshot
  3. Resizing it in my application
  4. Storing it in the RMS. (doesn't cause me problems)

 

Here are part of my code : 

 

  1. Start Camera & Listen to the FileSystem

 

    public CameraBlackBerry() {
//            invoking camera
        UiApplication.getApplication().addFileSystemJournalListener(this);
        Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments());
    }

2.  Get path to the new picture and transfering it to another class

 

    public void fileJournalChanged() {
        AIDOO.debug(">>>>>>>> FS MODIFIED !!!");
        Application app = UiApplication.getApplication();
        app.removeFileSystemJournalListener(this);
        long USN = FileSystemJournal.getNextUSN();
        for (long i = USN - 1; i >= 0; i--) {
            System.out.println(">>>>>>>> FS MODIFIED !!! : " + i);
            FileSystemJournalEntry entry = FileSystemJournal.getEntry(i);
            if (entry.getEvent() == FileSystemJournalEntry.FILE_ADDED) {
                path = entry.getPath();
                if (path.endsWith(".jpg")) {// Try to kill camera app here by injecting esc. HAD to remove condition '&& path.indexOf("IMG") >= 0' because changed in OS6
                    //#ifdef DebugMode
                    AIDOO.debug(">>>>>>>> Found a file added ending with jpg @  :" + path);
                    //#endif
                    // Get out of camera app
                    EventInjector.KeyEvent inject = new EventInjector.KeyEvent(EventInjector.KeyEvent.KEY_DOWN, Characters.ESCAPE, 0);
                    inject.post();
                    inject.post();
                    app.requestForeground();
                    AIDOO.currentCameraBlackBerry = null;
                    new CameraBlackBerryFileHandler(path);
                    break;
                }
            }
            if (entry == null) {
                // we didn't find an entry.
                break;
            }
        }
    }

3. Getting a FileConnection

 

    private FileConnection openFile() {
        try {
            FileConnection file = (FileConnection) Connector.open("file://" + path);
            if (file != null) {
                boolean fileExists = file.exists();
                int existCount = 0;
                while (!fileExists && existCount < 50) {
                    AIDOO.debug("fileExists sleeping for 100 msecond");
                    Thread.sleep(100);
                    fileExists = file.exists();
                    existCount++;
                }
                if (existCount < 50 && fileExists) {
                    return file;
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (InterruptedException ex) {
            alerta = new Alert(Lng.getString(Lng.a_alert), "InterruptedException while opening picture connection : " + ex.getMessage() + "  " + ex.getClass() + " path :" + path, null, null);
            AIDOO.handleError("Exception while opening picture connection", ex);
            return null;
        } catch (IllegalArgumentException ex) {
            alerta = new Alert(Lng.getString(Lng.a_alert), "IllegalArgumentException while opening picture connection : " + ex.getMessage() + "  " + ex.getClass() + " path :" + path, null, null);
            AIDOO.handleError("Exception while opening picture connection", ex);
            return null;
        } catch (ConnectionNotFoundException ex) {
            alerta = new Alert(Lng.getString(Lng.a_alert), "ConnectionNotFoundException while opening picture connection : " + ex.getMessage() + "  " + ex.getClass() + " path :" + path, null, null);
            AIDOO.handleError("Exception while opening picture connection", ex);
            return null;
        } catch (IOException ex) {
            alerta = new Alert(Lng.getString(Lng.a_alert), "IOException while opening picture connection : " + ex.getMessage() + "  " + ex.getClass() + " path :" + path, null, null);
            AIDOO.handleError("Exception while opening picture connection", ex);
            return null;
        }
    }

 

 4.  Obtaining the stream

 

    private DataInputStream tryAccessingImage(FileConnection file) {
        DataInputStream is = null;
        nbBytes = 0;
        try {
            is = file.openDataInputStream();
        } catch (IOException ex) {
            return null;
        }
        if (is != null && file.canRead()) {
            try {
                nbBytes = file.fileSize();
                isAvailNbBytes = is.available();
                int unreadableCount = 0;
                while (nbBytes <= 0 && unreadableCount < 50) {
                    AIDOO.debug("fileSize problem, sleeping for 100 msecond");
                    Thread.sleep(100);
                    nbBytes = file.fileSize();
                    unreadableCount++;
                }
                if (unreadableCount < 50) {
                    return is;
                } else {
                    return null;
                }
            } catch (InterruptedException ex) {
                String problem = Lng.getString(Lng.pic_couldnt_be_stored) + "(2) " + ex.getMessage() + "/ InterruptedException can read file : " + file.canRead() + " " + nbBytes + "bytes / " + path;
                append(problem, imageProblem);
                alerta = new Alert(Lng.getString(Lng.a_alert), problem, null, null);
                return null;
            } catch (IOException ex) {
                String problem = Lng.getString(Lng.pic_couldnt_be_stored) + "(2) " + ex.getMessage() + "/ IOException2 can read file : " + file.canRead() + " " + nbBytes + "bytes / " + path;
                append(problem, imageProblem);
                alerta = new Alert(Lng.getString(Lng.a_alert), problem, null, null);
                return null;
            }
        } else {
            alerta = new Alert(Lng.getString(Lng.a_alert), Lng.getString(Lng.pic_couldnt_be_stored) + "(2) / InterruptedException can read file : " + file.canRead() + " " + nbBytes + "bytes / " + path, null, null);
            return null;
        }
    }

 

5. Reading the stream

 

    private byte[] readingImage(DataInputStream is) {
        byte[] raw = new byte[(int) nbBytes];
        isReadBytes = -2; // -2 to differ from eventual -1, 0 or n (positive) returned by read()
        try {
            isReadBytes = is.read(raw);            
            return raw;
        } catch (IOException ex) {
            String problem = Lng.getString(Lng.pic_couldnt_be_stored) + "readingImage " + ex.getMessage() + "/ IOException1 can read file : " + nbBytes + "bytes / " + path;
            append(problem, imageProblem);
            alerta = new Alert(Lng.getString(Lng.a_alert), problem, null, null);
            return null;
        }
    }

 

6. Then i do the following

 

try {
                        is.close();
                        screenWidth /= 4;
                        screenWidth *= 3;
                        screenHeight /= 4;
                        screenHeight *= 3;
                        append("resizing image", imageBlank16);
                        raw = resizeImage(raw, 640, 480);
                        append("resize for preview image", imageBlank16);
                        byte rawPreview[] = resizeImage(raw, screenWidth, screenHeight);
                        AIDOO.debug("ok here");
                        append("creating preview image", imageBlank16);
                        imagePreview = Image.createImage(rawPreview, 0, rawPreview.length);
                        AIDOO.debug("ok here2");
                        String devinceInfo = DeviceInfo.getSoftwareVersion();
                        AIDOO.debug(devinceInfo);
                        AIDOO.debug("ok here3");
                        //#if !BlackBerry_OS_46 && !BlackBerry_Older_than_46
                        //raw = resizeImage(raw);
                        nbBytes = raw.length;
                        //#endif
                        if (nbBytes > CAMERA_IMAGE_MAX_SIZE) {
                            alerta = new Alert(Lng.getString(Lng.a_alert), Lng.getString(Lng.pic_size_is_too_large) + " : " + nbBytes, null, null);
                            System.out.println("error 2 after insert");
                            returnValue = false;
                        }
                    } catch (IOException ioe) {
                        alerta = new Alert(Lng.getString(Lng.a_alert), "IOException : " + ioe.getMessage() + "available Bytes : " + nbBytes, null, null);
                        AIDOO.handleError("IO problem while saving picture 0", ioe);
                        System.out.println("error 2 after insert");
                        returnValue = false;
                    }

 7. Here's my resizing image function 

 

    private byte[] resizeImage(byte[] image, int width, int height) {
        AIDOO.debug("Start of image resizing");
        Bitmap bmp1 = null, bmp2 = null;
        try {
            bmp1 = Bitmap.createBitmapFromBytes(image, 0, -1, 1);
            bmp2 = new Bitmap(width, height);
        } catch (IllegalArgumentException ex) {
            throw new IllegalArgumentException("exception while createBitmapFromBytes()" + ex.getMessage() + "isAvailNbBytes = " + isAvailNbBytes + " isReadBytes = " + isReadBytes + " length = " + image.length + " nbBytes = " + nbBytes + " image = " + image);
        }
        AIDOO.debug("Scale into");
        bmp1.scaleInto(bmp2, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT);
        AIDOO.debug("End of Scale into");
        JPEGEncodedImage encoded = JPEGEncodedImage.encode(bmp2, 90);
        return encoded.getData();
    }

 

 

My problems are : 

 

1. Sometime image is cropped, this size (w/h) is OK, but for some strange reason this doesn't appear all the time. There is always a large black zone at the bottom of the picture. It looks like that something hasn't been properly readen.

2. Sometimes, i got an IllegalArgumentException in the createBitmapFromBytes() function.

 

I read lots of posts that talks about those problems, but i didn't find any solution.

 

Is that that hard to take a picture from the camera and displaying it ?

Does anyone has a good example/code sample on the way to handle this ?

 

 

Thanks a lot for your help and for having read me

 

(Sorry for my english :smileytongue:)

gbe
Contributor
gbe
Posts: 22
Registered: ‎08-14-2009
My Device: BlackBerry Pearl

Re: Taking picture with camera...

[ Edited ]

Here are examples of one of my problems : 

 

bad_image.jpg

 

 

badimage.jpg

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Taking picture with camera...

I suspect these problems are caused by the fact that the journal listener you are using is told about the file being written before the write has actually completed. 

 

I think Ihad this problem in an application I wrote and to get round it, instead of processing the image in the JournalListener, I actually got the JournalListener to log the file created, and in the invoking screen's 'onExposed' method, I checked to see if the file name had been updated and if so, I would process it then.  onExposed will not get driven until the camera application has closed and at that time I believe it is save to say the file has been saved. 

 

I think there are other options.  For example, you could start a Thread checking the size of the file and if it doesn't grow after a second or so, you could assume that the write had been  completed.

 

Anyway, that is where I would start looking for the problem. 

gbe
Contributor
gbe
Posts: 22
Registered: ‎08-14-2009
My Device: BlackBerry Pearl

Re: Taking picture with camera...

Thank you very much for your help and for having read me.

 

Everything seems to work fine now

 

peter_strange, you'd helped me in most of my problems, many many thanks !

Developer
satish4ru
Posts: 329
Registered: ‎12-17-2010
My Device: Not Specified

Re: Taking picture with camera...

I am also getting the same problem what he is getting .. 

 

If i select large image i am getting output like that when i select medium or small images i am getting fine... 

 

What peter said i didnt understand that please help me out .. is there any way to change the camera setting from coding ( to change image size to small or medium) .... 

gbe
Contributor
gbe
Posts: 22
Registered: ‎08-14-2009
My Device: BlackBerry Pearl

Re: Taking picture with camera...

He's just telling that the listener is triggered too quickly.

 

So the photo is being written, and not entirely written, and in this time, the Listener is already woken up !

 

So you start your reading, on smthing not finished.

 

His advice was so to retry the read procedure (with a wait period) until the file has stopped from growing up.

 

Developer
satish4ru
Posts: 329
Registered: ‎12-17-2010
My Device: Not Specified

Re: Taking picture with camera...

hi ..

 

I am getting some problem when the image size to large means ( > 700 kb) . actually i want to send this image to server please help me how to decrease the encoded image size please help out .... when image is size is less that 700kb it is posting image fine but when image has high size then i am getting some unknown error please help me.. 

gbe
Contributor
gbe
Posts: 22
Registered: ‎08-14-2009
My Device: BlackBerry Pearl

Re: Taking picture with camera...

I just posted above a resizeImage method...

 

What's wrong with it ?

Developer
satish4ru
Posts: 329
Registered: ‎12-17-2010
My Device: Not Specified

Re: Taking picture with camera...

it is working fine .. 

 

i need how to compress image to send to server.... 

gbe
Contributor
gbe
Posts: 22
Registered: ‎08-14-2009
My Device: BlackBerry Pearl

Re: Taking picture with camera...

Is it compressed properly ?

 

There are no miracle, if you want to send a 5MP picture you'd have to resize it, or to raise the compression