Android onPictureTaken未被调用

时间:2012-04-24 14:27:31

标签: android android-widget hardware

我正在尝试制作一个可以拍照的屏幕。这张照片将转到数据库。问题是,我尝试了多种方法,在每种方法中,我都需要使用PictureCallback。但无论如何都不会调用来自PictureCallback的onPictureMethod。

我已经尝试过调查thisthis,但仍然无法移动。

编辑:使用Android 2.2(API 8)

编辑代码:

package touchcare.idealogix.android;

//imports

public class CameraActivityTest extends Activity implements SurfaceHolder.Callback
{
private SurfaceView surfaceCamera;
private SurfaceHolder holderCamera;
private Camera camera;
private Button btnCapture;
private PictureCallback mPictureJpg;
private PictureCallback mPictureRaw;
private ShutterCallback shutterCallback;

@Override public void onCreate(Bundle bundle){
    super.onCreate(bundle);
    setContentView(R.layout.layout_camera);

    Log.d("PICTURE", "ACTIVITY START");

    surfaceCamera = (SurfaceView) findViewById(R.id.surfaceCamera);
    btnCapture = (Button) findViewById(R.id.btnCapture);
    holderCamera =  surfaceCamera.getHolder();

    holderCamera.addCallback(this);
    holderCamera.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    shutterCallback = new ShutterCallback() 
    {   
        @Override
        public void onShutter() 
        {
            //DONOTHING
        }
    };

    mPictureRaw = new PictureCallback() 
    {   
        @Override
        public void onPictureTaken(byte[] data, Camera camera)
        {
            //DONOTHING
        }
    };

    mPictureJpg = new PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) 
        {
            Log.d("PICTURE", "ON PICTURE TAKEN START");

            Intent intent = getIntent();

            final boolean beds = intent.getExtras().getBoolean(Main.BEDS, false);
            final boolean nurse = intent.getExtras().getBoolean(Main.NURSE, false);
            final int position = intent.getExtras().getInt(Main.POSITION, 0);

            if(beds)
            {
                BedroomDataSource bedDs = new BedroomDataSource(CameraActivityTest.this);
                String where = SQLiteBedroomHelper.COLUMN_UID + "=" + AppData.getBedrooms().get(position).getUid();
                bedDs.open();
                bedDs.updateBedroom(where, null, null, null, null, null, null, data);
                bedDs.close();
                Log.d("PICTURE", "BEDS BOOLEAN");
                AppData.getBedrooms().get(position).setImage(data);
            }else if(nurse)
            {

            }

            Log.d("PICTURE", "ON PICTURE TAKEN END");

            finish();
        }
    };

    btnCapture.setOnClickListener(new View.OnClickListener() 
    {   
        @Override
        public void onClick(View arg0) 
        {               
            camera.takePicture(shutterCallback, mPictureRaw, mPictureJpg);
        }
    });
}

@Override public void surfaceCreated(SurfaceHolder holder)
{
    camera = Camera.open();

    Camera.Parameters params = camera.getParameters();      

    params.setPreviewSize(holder.getSurfaceFrame().width(), holder.getSurfaceFrame().height());
    params.setPictureSize(32, 32);
    params.setJpegQuality(100);

    camera.setParameters(params);

    try
    {
        camera.setPreviewDisplay(holder);
    }catch(IOException ex){
        Log.d("PICTURE", "PREVIEW EXCEPTION");
    }

    camera.startPreview();

    Log.d("PICTURE", "SURFACE CREATED");
}

@Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
    Log.d("PICTURE", "SURFACE CHANGED");
}

@Override public void surfaceDestroyed(SurfaceHolder holder)
{
    camera.stopPreview();

    camera.release();

    Log.d("PICTURE", "SURFACE DESTROYED");
}
}

感谢。

编辑:看起来拍摄了一些照片并且达到了回调。数据库获取照片,并在位图中加载回来就好了。但根据我拍的照片,回调没有到达。更小更简单的事情是100%有可能执行回调。

1 个答案:

答案 0 :(得分:-1)

看起来您创建了回调mPicture,然后从未将其设置为任何回调。我从来没有使用任何图片的东西,所以我不能建议在哪里设置它,但据我所知你需要注册回调它才能被调用,只是定义它对你没有任何好处。