Android相机的麻烦

时间:2010-12-03 04:20:09

标签: java android camera

我有一些代码,我一直在试验,看看我能用相机设备做些什么。以下代码有效,但我有一些问题似乎无法解决。

  1. 第一个电话永远不会有效。第一次运行代码时,从不调用onPictureTaken回调,因此永远不会写入文件。然而,相机会完成所有其他步骤,包括制作假快门噪音。

  2. 我似乎无法将图片大小设置为默认值之外的其他内容。如果我尝试将其设置为其他内容,代码将停止工作。与上面相同,相机经过所有动作,但onPictureTaken回调从未被调用过。

  3. 当照片保存到DCIM文件夹时,拍摄的照片不会显示在手机的默认“照片”应用中,除非我重新启动手机。

  4. 是否有通过代码来禁用快门噪音的方法?

  5. 很抱歉,代码有点乱,因为它是一个实验。

    此外,此代码在BroadcastReceiver

    中执行
        @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
    
    
        if(intent.getAction().equals(TAKE_PICTURE_INTENT))
        {
    
    
            Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();
    
    
                    System.out.println("GOT THE INTENT");
    
                        try
                        {
                            Camera camera = Camera.open();
    
                            System.out.println("CAMERA OPENED");
    
                            Parameters params = camera.getParameters();
                            params.set("flash-mode", "off");
                            params.set("focus-mode", "infinity");
                            params.set("jpeg-quality", "100");
                            //params.setPictureSize(2592, 1952);
    
                            String str = params.get("picture-size" + "-values");
                            System.out.println(str);
    
                            String size = str.split(",")[0];
                            System.out.println(size);
    
                            //params.set("picture-size", size);
    
                            camera.setParameters(params);
    
    
                            System.out.println("CAMERA PARAMETERS SET");
    
    
    
                            camera.startPreview();
                            System.out.println("CAMERA PREVIEW STARTED");
    
    
                            camera.autoFocus(new AutoFocusCallBackImpl());
    
    
                        }
                        catch(Exception ex)
                        {
                            System.out.println("CAMERA FAIL, SKIP");
                            return ;
                        }
    
    
        }//if   
    
    }//onreceive
    
    
    private void TakePicture(Camera camera)
    {
        camera.takePicture(new Camera.ShutterCallback() {
    
            @Override
            public void onShutter() {
                // TODO Auto-generated method stub
    
                System.out.println("CAMERA SHUTTER CALLBACK");
            }
        }
         , null, 
            new Camera.PictureCallback() {
    
                    public void onPictureTaken(byte[] imageData, Camera c) {
                        //c.release();
    
                        System.out.println("CAMERA CALLBACK");
    
                        FileOutputStream outStream = null;
                        try {
    
                            System.out.println("Start Callback");
                            File esd = Environment.getExternalStorageDirectory();
    
                            outStream = new FileOutputStream(esd.getAbsolutePath() + String.format(
                                    "/DCIM/%d.jpg", System.currentTimeMillis()));
    
                            outStream.write(imageData);
                            outStream.close();
                            System.out.println( "onPictureTaken - wrote bytes: " + imageData.length);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
    
                              System.out.println("File not found exception");
                        } catch (IOException e) {
                            e.printStackTrace();
    
                              System.out.println("IO exception");
                        } finally {
                              System.out.println("Finally");
                              c.release();
                        }
    
                    }
                }
             );
        //camera.release();
    }//TAKE PICTURE
    
    private class AutoFocusCallBackImpl implements Camera.AutoFocusCallback {
        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            //bIsAutoFocused = success; //update the flag used in onKeyDown()
            System.out.println("Inside autofocus callback. autofocused="+success);
            //play the autofocus sound
            //MediaPlayer.create(CameraActivity.this, R.raw.auto_focus).start();
    
            if(success)
            {
                System.out.println("AUTO FOCUS SUCCEDED");
            }
            else
            {
                System.out.println("AUTO FOCUS FAILED");
            }
    
            TakePicture(camera);
            System.out.println("CALLED TAKE PICTURE");
    
        }
     }//AUTOFOCUSCALLBACK
    

1 个答案:

答案 0 :(得分:1)

1.首先,将所有摄像机逻辑从BroadCast接收器和放大器中取出。把它放到seprate Activity。

2

  

当照片保存到DCIM文件夹时,拍摄的照片不会显示在手机的默认“照片”应用中,除非我重新启动手机。

因为拍照后需要调用MediaScanner重新扫描图像/更改。当你重新启动手机mediascanner扫描媒体&找到新的图像。为此,您应该查看MediaScanner。

3.关注Android相机Tutorial& Camera API

-Thanks