0

我对 Android 中的相机功能没有太多经验。我需要通过我计划发送回服务器的代码拍照。我的逻辑基于这篇文章,只要我不添加mCamera.takePicture(null, null, mPictureCallback);到 surfaceChange 方法的末尾(这是我想我猜的),它就可以很好地工作。当我添加该行时,它有时会起作用,但大多数时候,我只是返回一个black screen。这是主要问题。我不认为该设备存在兼容性问题,因为我至少提前 3-4 次获得了此功能。

我正在测试的设备是 Galaxy Nexus

public class CameraView extends Activity implements SurfaceHolder.Callback, OnClickListener
{
    private static final String TAG = "CameraTest";
    Camera mCamera;
    boolean mPreviewRunning = false; 

    public void onCreate(Bundle icicle) 
    {
        super.onCreate(icicle);
        Log.e(TAG, "onCreate");

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_camera_view);
        mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        mSurfaceView.setOnClickListener(this);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) 
    {
        super.onRestoreInstanceState(savedInstanceState);
    }


    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
            if (data != null) 
            {
                //Intent mIntent = new Intent();
                //mIntent.putExtra("image",imageData);

                mCamera.stopPreview();
                mPreviewRunning = false;
                mCamera.release();


                 try
                   {
                   BitmapFactory.Options opts = new BitmapFactory.Options();
                   Bitmap bitmap= BitmapFactory.decodeByteArray(data, 0, data.length,opts);
                   bitmap = Bitmap.createScaledBitmap(bitmap, 480, 480, false);
                   CameraProjectActivity.image.setImageBitmap(bitmap);
                   }
                   catch(Exception e)
                   {
                         e.printStackTrace();
                   }
                //StoreByteImage(mContext, imageData, 50,"ImageName");
                //setResult(FOTO_MODE, mIntent);
                setResult(585);
                finish();
            }       
        }
    };




    protected void onResume() 
    {
        Log.e(TAG, "onResume");
        super.onResume();
    }

    protected void onSaveInstanceState(Bundle outState) 
    {
        super.onSaveInstanceState(outState);
    }

    protected void onStop() 
    {
        Log.e(TAG, "onStop");
        super.onStop();
    }

    public void surfaceCreated(SurfaceHolder holder) 
    {
        Log.e(TAG, "surfaceCreated");
        mCamera = Camera.open();
        mCamera.setDisplayOrientation(90);

    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Log.e(TAG, "surfaceChanged");

        // XXX stopPreview() will crash if preview is not running
        if (mPreviewRunning) 
        {
            mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();

        List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();

        Camera.Size previewSize = previewSizes.get(3);
        p.setPreviewSize(previewSize.width, previewSize.height);


        mCamera.setParameters(p);
        try 
        {
            mCamera.setPreviewDisplay(holder);
        } 
        catch (Exception e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mCamera.startPreview();
        mPreviewRunning = true;

    // THIS IS THE CODE THAT BREAKS IT. IS THERE ANY OTHER WAY TO DO THIS??? ********
    // mCamera.takePicture(null, null, mPictureCallback);


    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.e(TAG, "surfaceDestroyed");
//        mCamera.stopPreview();
//        mPreviewRunning = false;
//        mCamera.release();
    }

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder; 

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        mCamera.takePicture(null, mPictureCallback, mPictureCallback);
    }
}

任何帮助,将不胜感激。谢谢!

4

2 回答 2

2

我不确定这是否是一个好的解决方案,但我只是通过同步 surfaceChanged() 方法并在调用 takePicture 之前让线程等待一秒钟来修复它。

try {
    this.wait(1000);
    mCamera.takePicture(null, null, mPictureCallback);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
于 2013-02-02T23:43:51.413 回答
0

我相信建议在startPreview()之前调用setPreviewDisplay(),但这应该不会产生重大影响。

您看到的是相机初始化需要时间,并且在准备好之前无法拍照。最好使用来自相机的回调来确定它已准备好,而不是等待任意 1000 毫秒。

您可以使用的回调是Camera.PreviewCallback。您可以在CameraView类中实现它。要触发回调,只需将setOneShotPreviewCallback(this)添加到CameraView.surfaceChanged()

mCamera.startPreview();
mCamera.setPreviewDisplay(holder);
mCamera.setOneShotPreviewCallback(this);
于 2013-02-03T08:28:25.200 回答