Android:相机:jpeg回拨图片数据null

时间:2013-10-08 14:37:20

标签: android camera

我有以下代码块从前置摄像头捕获图像并将其存储到存储中。下面的代码适用于我尝试过的所有设备(三星Galaxy S2,Note 2,Micromax Canvas 2,HD,Sony Xperia U),但是对于HTC One X,jpegcallback返回的“数据”始终为空。任何想法为什么?

public void takePicture () {
        Log.d("camera","taking picture");
        // do we have a camera?
        if (cameraIsOk()) {
            Log.d("camera","waiting 1");

            try {
                synchronized (this) {
                   wait(2000);
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                Log.d("camera", "Waiting didnt work!!");
                e.printStackTrace();
            }
            if (myCamera == null) 
                myCamera = Camera.open(camId);

            if (myCamera != null)
            {
                Log.d("camera","surface view");
                SurfaceView surfaceView = new SurfaceView(context);
                surfaceView.setFocusable(true);
                Log.d("camera","getting holder");
                SurfaceHolder holder = surfaceView.getHolder();
                Log.d("camera","add callback");
                holder.addCallback(this);
                Log.d("camera","set type");
                holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

                try {
                    Log.d("camera","get parameters");
                    Camera.Parameters parameters = myCamera.getParameters();
                    Log.d("camera","get supported preview size");
                    List<Size> sizes = parameters.getSupportedPreviewSizes();
                    Size optimalSize = GBCameraUtil.getOptimalPreviewSize(sizes, 1024, 768);

                    int sdkBuildVersion = Integer.parseInt( android.os.Build.VERSION.SDK );

                    if (sdkBuildVersion < 5 || usingLandscape) 
                    {
                        // Picture size should be landscape
                        if (optimalSize.width < optimalSize.height || usingLandscape)
                            parameters.setPictureSize( optimalSize.height, optimalSize.width );
                        else
                            parameters.setPictureSize( optimalSize.width, optimalSize.height );
                    }
                    else
                    {
                        // If the device is in portraint and width > height, 
                        // or if the device is in landscape and height > height, so we need to rotate them.
                        switch (context.getResources().getConfiguration().orientation) {
                        case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :
                            if (optimalSize.height > optimalSize.width ) {
                                parameters.setRotation(camId == GBCameraUtil.findFrontFacingCamera() ? 270 : 90);
                            }

                            break;                               
                        case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :                     
                            if (optimalSize.width > optimalSize.height) {
                                parameters.setRotation(camId == GBCameraUtil.findFrontFacingCamera() ? 270 : 90);
                            }

                            break;                               
                        }  

                        parameters.setPictureSize (optimalSize.width, optimalSize.height);
                    }
                    Log.d("camera","set param");
                    myCamera.setParameters(parameters); 
                    Log.d("camera","set preview disp");

                    myCamera.setPreviewDisplay(holder);
                    Log.d("camera","start preview");
                    myCamera.startPreview(); 
                    Log.d("camera","take picture");
                    myCamera.takePicture(null, null, getJpegCallback());


                } catch (Exception e) {
                    // Sorry, nothing to do
                }
            }
        }
    }

    private PictureCallback getJpegCallback(){
        PictureCallback jpeg=new PictureCallback() {   
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                myCamera.stopPreview();
                mPreviewRunning = false;
                Log.d("camera","pic taken");

                if (data != null) {
                    Toast.makeText(context, "data not null ", Toast.LENGTH_LONG).show();
                    Log.d("camera","data not null. Filename is :" + fileName);


                    FileOutputStream fos;
                    try {
                        if (fileName.equals("")) {
                            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
                            String date = dateFormat.format(new Date());
                            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                                fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "picture" + date + ".jpg";
                            }
                            else{
                                Toast.makeText(context, "storing to internal " , Toast.LENGTH_LONG).show();
                                //  fileName = Environment.getDataDirectory().getAbsolutePath() + "/" + "picture" + date + ".jpg";
                                fileName = context.getFilesDir().getAbsolutePath()+ "/" + "unlocked" + date + ".jpg";
                            }
                            Toast.makeText(context, "filename " + fileName, Toast.LENGTH_LONG).show();
                            Log.d("camera","filename is : " + fileName);
                        }
                        Log.d("camera","coming here");
                        fos = new FileOutputStream(new File(fileName));
                        Log.d("camera","writing data");

                        fos.write(data);
                        Log.d("camera","written data");
                        fos.close();
                    }  catch (IOException e) {
                        //do something about it
                        Log.d("camera","some exception"+ e);
                    }


                }
                else
                    Toast.makeText(context, "data  null " , Toast.LENGTH_LONG).show();

                myCamera.release();
                myCamera = null;    
            }
        };

        return jpeg;
    }

2 个答案:

答案 0 :(得分:0)

也许问题是您询问了支持的预览尺寸列表。难道HTC One X支持的图片大小不同吗? 这不是问题所在。看到另一个答案。

答案 1 :(得分:0)

我注意到您的onPictureTaken()来电camera.stopPreview()。据我所知,它不应该。

请参阅Camera.takePicture

此方法仅在预览处于活动状态时(startPreview()之后)有效。拍摄图像后预览将停止;如果用户想要重新开始预览或拍摄更多照片,则必须再次致电startPreview()。不应在start()stop()之间调用此方法。

粗体是我的。致电camera.stopPreview()后,您不应致电camera.takePicture()。系统会为您做到这一点。碰巧是这样,有些设备可能会原谅意外通话,但 HTC One X 则不会。