繁体   English   中英

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

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

我想编写一种相机应用程序,该应用程序使用从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版本上运行,那么它也可以在新版本上运行。

谢谢

API级别9具有打开的公共静态Camera(int cameraId),其中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