2012-05-02 1 views
2

Je fais une application dans laquelle l'utilisateur pourra utiliser l'appareil photo, capturer et enregistrer cette image sur le lecteur C, et je suis également capable d'effectuer tout cela lorsque j'utilise cette application avec mon PC. Mais à chaque fois que j'utilise cette application dans les mobiles Comme Nokia C2-01,02,03 je suis seulement pour voir l'appareil photo mais pas en mesure de capturer une image en courte capture ne fonctionne pas pendant que j'utilise Mobile pour exécuter cette application.Impossible de capturer une image en utilisant Nokia Mobile mais dans l'application informatique fonctionne bien?

Code Mon Midlet est ci-dessous s'il vous plaît voir le problème et me soutenir pour capturer l'image via Mobile aussi: -

public class CaptureAndSaveImage extends MIDlet implements CommandListener { 

    private Display display; 

    // Form where camera viewfinder is placed 
    private Form cameraForm; 

    // Command for capturing image by camera and saving it. 
    // Placed in cameraForm. 
    private Command cmdCapture; 
    // Command for exiting from midlet. Placed in cameraForm. 
    private Command cmdExit; 

    // Player for camera 
    private Player player; 
    // Video control of camera 
    private VideoControl videoControl; 

    // Alert to be displayed if error occurs. 
    private Alert alert; 

    /** 
    * Constructor. 
    */ 
    public CaptureAndSaveImage() { 
     InitializeComponents(); 
    } 

    /** 
    * Initializes components of midlet. 
    */ 
    private void InitializeComponents() { 
     display = Display.getDisplay(this); 

     if(checkCameraSupport() == false) { 
      showAlert("Alert", "Camera is not supported!", null); 
      return; 
     } 

     try { 
      createCameraForm(); 
      createCamera(); 
      addCameraToForm(); 
      startCamera(); 
     } catch(IOException ioExc) { 
      showAlert("IO error", ioExc.getMessage(), null); 
     } catch(MediaException mediaExc) { 
      showAlert("Media error", mediaExc.getMessage(), null); 
     } 
    } 

    /** 
    * Creates and returns form where the camera control will be placed. 
    */ 
    private void createCameraForm() { 
     // Create camera form 
     cameraForm = new Form("Camera"); 
     // Create commands for this form 
     cmdCapture = new Command("Capture", Command.OK, 0); 
     cmdExit = new Command("Exit", Command.EXIT, 0); 
     // Add commands to form 
     cameraForm.addCommand(cmdCapture); 
     cameraForm.addCommand(cmdExit); 
     // Set midlet as command listener for this form 
     cameraForm.setCommandListener(this); 
    } 

    /** 
    * Check camera support. 
    * @return true if camera is supported, false otherwise. 
    */ 
    private boolean checkCameraSupport() { 
     String propValue = System.getProperty("supports.video.capture"); 
     return (propValue != null) && propValue.equals("true"); 
    }  

    /** 
    * Creates camera control and places it to cameraForm. 
    * @throws IOException if creation of player is failed. 
    * @throws MediaException if creation of player is failed. 
    */ 
    private void createCamera() throws IOException, MediaException { 
     player = Manager.createPlayer("capture://video"); 
     player.realize(); 
     player.prefetch(); 

     videoControl = (VideoControl)player.getControl("VideoControl"); 
    } 

    /** 
    * Adds created camera as item to cameraForm. 
    */ 
    private void addCameraToForm() { 
     cameraForm.append((Item)videoControl. 
       initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null)); 
    } 

    /** 
    * Start camera player 
    * @throws IOException if starting of player is failed. 
    * @throws MediaException if starting of player is failed. 
    */ 
    private void startCamera() throws IOException, MediaException { 
     if(player.getState() == Player.PREFETCHED) { 
      player.start(); 
     } 
    } 

    /** 
    * Saves image captured by camera. 
    */ 
    private void captureAndSaveImage() { 
     FileConnection file = null; 
     OutputStream outStream = null; 

     try { 
      if(checkPngEncodingSupport() == false) { 
       throw new Exception("Png encoding is not supported!"); 
      } 

      // Capture image 
      byte[] capturedImageData = 
        videoControl.getSnapshot("encoding=png"); 

      // Get path to photos folder. 
      String dirPhotos = System.getProperty("fileconn.dir.photos"); 
      if(dirPhotos == null) { 
       throw new Exception("Unable get photos folder name"); 
      } 

      String fileName = dirPhotos + "CapturedImage.png"; 
      // Open file 
      file = (FileConnection)Connector.open(fileName, 
        Connector.READ_WRITE); 
      // If there is no file then create it 
      if(file.exists() == false) { 
       file.create(); 
      } 
      // Write data received from camera while making snapshot to file 
      outStream = file.openOutputStream(); 
      outStream.write(capturedImageData); 

      showAlert("Info", "Image is saved in " + fileName, cameraForm); 

     } catch(IOException ioExc) { 
      showAlert("IO error", ioExc.getMessage(), cameraForm); 
     } catch(MediaException mediaExc) { 
      showAlert("Media error", mediaExc.getMessage(), cameraForm); 
     } catch(Exception exc) { 
      showAlert("Error", exc.getMessage(), cameraForm); 
     } finally { 
      // Try to close file 
      try { 
       if(outStream != null) { 
        outStream.close(); 
       } 
       if(file != null) { 
        file.close(); 
       } 
      } catch(Exception exc) { 
       // Do nothing 
      } 
     } 
    }  

    /** 
    * Checks png encoding support 
    * @return true if png encoding is supported false otherwise. 
    */ 
    private boolean checkPngEncodingSupport() { 
     String encodings = System.getProperty("video.snapshot.encodings"); 
     return (encodings != null) && (encodings.indexOf("png") != -1); 
    } 

    /** 
    * From MIDlet. 
    * Signals the MIDlet that it has entered the Active state. 
    */ 
    public void startApp() { 
     if (videoControl != null) { 
      display.setCurrent(cameraForm); 
     } 
    }  

    /** 
    * From MIDlet. 
    * Signals the MIDlet to enter the Paused state. 
    */ 
    public void pauseApp() {   
     // TODO: pause player if it is running. 
    } 

    /** 
    * Performs exit from midlet. 
    */ 
    public void exitMIDlet() { 
     notifyDestroyed(); 
    } 

    /** 
    * Shows alert with specified title and text. If next displayable is not 
    * specified then application will be closed after alert closing. 
    * @param title - Title of alert. 
    * @param message - text of alert. 
    * @param nextDisp - next displayable. Can be null. 
    */ 
    private void showAlert(String title, String message, Displayable nextDisp) { 
     alert = new Alert(title); 
     alert.setString(message); 
     alert.setTimeout(Alert.FOREVER); 

     if(nextDisp != null) { 
      display.setCurrent(alert, nextDisp); 
     } else { 
      display.setCurrent(alert); 
      alert.setCommandListener(this); 
     } 
    }   

    /** 
    * From MIDlet. 
    * Signals the MIDlet to terminate and enter the Destroyed state. 
    */ 
    public void destroyApp(boolean unconditional) { 
     if(player != null) { 
      player.deallocate(); 
      player.close(); 
     } 
    } 

    /** 
    * From CommandListener. 
    * Indicates that a command event has occurred on Displayable displayable. 
    * @param command - a Command object identifying the command. 
    * @param displayable - the Displayable on which this event has occurred. 
    */ 
    public void commandAction(Command command, Displayable displayable) { 
     // Handles "Capture image" command from cameraForm 
     if(command == cmdCapture) { 
      captureAndSaveImage(); 
     } 
     // Handles "exit" command from forms 
     if(command == cmdExit) { 
      exitMIDlet(); 
     } 
     // Handle "ok" command from alert 
     if(displayable == alert) { 
      exitMIDlet(); 
     } 
    } 
} 

Répondre

2

Peut-être que vous devriez essayer de capturer une OutOfMemoryError (mieux attraper Throwable qui va attraper trop au lieu d'une exception) dans le bloc try-catch à captureAndSaveImage()

vous pouvez également voir fileName pour vous assurer qu'il essaie de sauver dans le répertoire approprié

showAlert("fileName", fileName, this); 
    // Open file 
+0

Hye Mr. Drive Maintenant, je reçois le PNG non pris en charge ...... – user1369219

+0

Cela signifie, bien sûr, qu'il n'est pas pris en charge par votre appareil cible. Essayez jpeg alors. –

1

Dans votre méthode creatCamera à l'endroit où vous créez un lecteur avec l'utilisation de la classe de gestionnaire jst, essayez le mode capture au lieu du mode vidéo.

player = Manager.createPlayer("capture://image"); 

J'ai rencontré le même problème dans nokia-c1. Et

byte[] capturedImageData = videoControl.getSnapshot(null); 

en passant nul argument de dans la méthode getSnapshot vous obtenez en retour un formate d'image pris en charge par défaut de votre appareil.

Questions connexes