Picture taken with the camera...

Hi all

I am currently facing a lot of problems to use the camera. After days and days of trying to change/adapt my code, you are my last hope...

What I want to do is:

  1. Departure from camera
  2. The take a snapshot
  3. Resizing in my application
  4. Store in the FET (don't cause me problems)

Here is part of my code:

  1. Beginning camera & look the file system
    public CameraBlackBerry() {
//            invoking camera
        UiApplication.getApplication().addFileSystemJournalListener(this);
        Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments());
    }

2 get path to the new image and transfer 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. how to get 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 getting 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 Behold my image resizing 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. sometimes the image is cropped, this size (w/h) is OK, but for some strange reason it is not all the time. There is always a large black area at the bottom of the image. It seems that something has not been correctly readen.

2. sometimes I had an IllegalArgumentException in createBitmapFromBytes() function.

I read a lot of posts that talk about these problems, but I have found no solution.

Is it so hard to take a picture of the camera and display it?

Anyone have a good example/code example on how to handle this?

Thank you for your support and for reading me

(Sorry for my English )

I suspect that these problems are caused by the fact that the earpiece of the paper you use is told on the file being written until the writing is actually completed.

I think that Ihad this problem in an application that I wrote and to get around it, rather than process the image in the JournalListener, I actually got the JournalListener to save the file created in the method of invocation of the "onExposed" screen, I checked to see if the name of the file has been updated and if so , I would treat it then.  onExposed is not get leads until the camera application has closed at this time, according to me, that it is save saying that the file has been saved.

I think that there are other options.  For example, you could start a thread check the size of the file and if it does not grow after a second or two, you might assume that the writing was completed.

In all cases, this is where I would start looking for the problem.

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for

  • Camileo S30 won't save

    It's the second s30 I've owned (first just died)... as a first step, this second o.k saved one but now when I press the record button nothing happens... it is equipped with a max ram32gb sdhc card that I formatted... Any suggestions?

  • FPGA FIFO real-time error-61206

    I used this white paper as a base for my code. My FPGA look like: And my time real-time (RT): I am using a cRIO-9022 and now just trying to get the foundations buried for my project. The problem is that the side FPGA works well but environmental RT t

  • Car USB charger question

    Bought a charger car Macally, model USBCIG2, that lists all compatible iPod and iPhone models.  My question is that will work with my Clip charger, it is standard 5v usb output. MacAlly also has a model for Sansa, but good luck to find in a store. I

  • Wallpaper images load oversized cut about 1 "of the original photo.

    As indicated, the background screen images are cut approximately 1 "autour. The original pictures are 1024 X 768 size. It seems as if the picture will leave the size of moose to 120%. Don't know if this has changed on the computer since I don't chang

  • When 8 7840 place will get a lollipop?

    I was wondering as I thought and mine, dell said it must come preloaded. Nope... KitKat.