简体   繁体   English

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

[英]Application works on android 2.2, but not on 2.3

I wanna write a kind of camera application which uses real-time preview from camera displayed on SurfaceView, another layer above preview is a mask (funny picture etc.). 我想编写一种相机应用程序,该应用程序使用从SurfaceView上显示的相机进行实时预览,预览上方的另一层是蒙版(有趣的图片等)。 The function to take a picture on display click. 点击显示拍照功能。 The issue is that the application works on adroid 2.2 just fine but does NOT work on 2.3 (using phone and emulator). 问题在于该应用程序可以在adroid 2.2上正常运行,但不能在2.3(使用电话和模拟器)上运行。 I have premission for camera in Mainfest. 我对Mainfest的相机情有独钟。

Here is my activivty: 这是我的活动:

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) {

        }};
}

And the classrelated to the camera preview 与摄像机预览有关的类

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();
    }
}

Any ideas what is wrong? 任何想法有什么问题吗? I thought taht if application works on older android version it works on the new one as well. 我认为如果应用程序可以在较旧的android版本上运行,那么它也可以在新版本上运行。

Thank you 谢谢

The API level 9 has public static Camera open (int cameraId) where cameraid define the front and rear camera. API级别9具有打开的公共静态Camera(int cameraId),其中cameraid定义了前置和后置摄像头。 please change the android jar for api level and change the method public Camera.open () to Camera open (int cameraId) 请更改api级别的android jar并将方法public Camera.open()更改为Camera open(int cameraId)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM