应用程序适用于Android 2.2,但不适用于2.3

时间:2012-03-04 19:23:16

标签: android android-2.2-froyo android-2.3-gingerbread

我想编写一种相机应用程序,它使用SurfaceView上显示的相机进行实时预览,预览上方的另一层是一个面具(有趣的图片等)。在显示屏上拍照的功能点击。问题是该应用程序适用于adroid 2.2,但不适用于2.3(使用手机和模拟器)。我在Mainfest有摄像头。

这是我的活动:

package funny.camera;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class CameraActivity extends Activity{

    private CamScreen cam;
    private LayoutInflater controlInflater = null;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    View touchscreen;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cam = new CamScreen(this);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

        setContentView(R.layout.camscreen);

        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(cam);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);     

        controlInflater = LayoutInflater.from(getBaseContext());

        View viewControl = controlInflater.inflate(R.layout.camoverlay, null);
        LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        addContentView(viewControl, layoutParamsControl);
        View viewTouch = controlInflater.inflate(R.layout.camtouch, null);
        addContentView(viewTouch, layoutParamsControl);  

        viewControl.setOnClickListener(new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                cam.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
            }
        });
    }
    ShutterCallback shutterCallback = new ShutterCallback(){

        @Override
        public void onShutter() {

        }};

    PictureCallback rawCallback = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {

        }};

    PictureCallback jpegCallback = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] _data, Camera _camera) {

        }};
}

与相机预览相关的课程

public class CamScreen extends SurfaceView implements SurfaceHolder.Callback {

    //SurfaceHolder sHolder;
    //SurfaceView surfaceView;
    Camera camera;

    CamScreen(Context context) {
    super(context);    
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // Open the camera and start viewing    
        camera = Camera.open();

        try {
           camera.setPreviewDisplay(holder);
        } catch (IOException exception) {
            camera.release();
            camera = null;
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Kill all our crap with the surface
        camera.stopPreview();
        camera.release();
        camera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Modify parameters to match size.
        Camera.Parameters params = camera.getParameters();
        params.setPreviewSize(w, h);
        params.setPictureFormat(PixelFormat.JPEG);
        camera.setParameters(params);

        camera.startPreview();
    }
}

任何想法有什么不对?我认为,如果应用程序适用于较旧的Android版本,它也适用于新版本。

谢谢

1 个答案:

答案 0 :(得分:1)

API级别9打开公共静态相机(int cameraId) cameraid定义前后摄像头。请更改api级别的android jar并将方法public Camera.open()更改为Camera open(int cameraId)