无法使用诺基亚手机拍摄图像,但在计算机应用程序中工作正常?

Posted

技术标签:

【中文标题】无法使用诺基亚手机拍摄图像,但在计算机应用程序中工作正常?【英文标题】:Not able to capture an image using Nokia Mobile but in Computer application works fine? 【发布时间】:2012-05-02 05:48:42 【问题描述】:

我正在制作一个应用程序,用户可以在其中使用相机,捕捉图像并将其保存到 C 盘,当我在 PC 上使用此应用程序时,我也能够执行所有这些操作。

但每当我在移动设备中使用此应用程序时,例如诺基亚 C2-01,02,03,我只能查看相机但无法在短时间捕获图像,但我使用移动设备运行此应用程序时无法正常工作。

我的 Midlet 代码如下,请查看问题并支持我通过移动设备捕获图像:-

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();
        
    

【问题讨论】:

【参考方案1】:

也许您应该尝试在 captureAndSaveImage() 的 try-catch 块中捕获 OutOfMemoryError(最好捕获 Throwable,它也会捕获它而不是异常)

您可能还想查看 fileName 以确保它尝试保存在正确的目录中

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

【讨论】:

当然,这意味着您的目标设备不支持它。那就试试jpeg吧。【参考方案2】:

在您创建播放器的 creatCamera 方法中,使用管理器类 jst 尝试捕获模式而不是视频模式。

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

我在 nokia-c1 中遇到了同样的问题。 还有

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

通过在 getSnapshot 方法中传递 null 参数,您将获得设备支持的默认图像格式。

【讨论】:

以上是关于无法使用诺基亚手机拍摄图像,但在计算机应用程序中工作正常?的主要内容,如果未能解决你的问题,请参考以下文章

jquery 下载图像(数据:图像)链接不适用于 Ipad,但在窗口中工作正常

文件上传选项以从相机拍摄图像或从图库中选择不适用于 Mozilla Firefox 中的 Web 应用程序

Firebase 存储图像无法正确上传

套接字连接在无线工具包中运行良好,但在我的诺基亚手机中没有

使用无法在 Safari 中工作的图像转换 SVG

如何在 Java ME 中增加堆大小?