无法在Android中正确显示相机视图?

时间:2011-12-12 13:35:33

标签: android camera rotation

在我的Android应用程序即时通讯中使用相机并以编程方式启动它。我在samsung gt-s5360上运行我的代码,但视图旋转了90度反时钟智能。我的代码在HTC野火-s上工作正常。这个设备相机视图正确显示而不旋转视图。但它没有在每个设备上显示正确的视图。所以我如何编写适用于所有设备的代码(android)。我的android jar是2.2,API级别是8请帮助我。这是我的代码。

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{
    int rotate=90;
    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;
    LayoutInflater controlInflater = null;
    public static ArrayList<Activity> activities=new ArrayList<Activity>();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        activities.add(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


        Intent i=new Intent();
        i.setClassName("a.b.c","a.b.c.DialPad");
        startActivity(i);

    }


    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        //this.camera.setDisplayOrientation(90);
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                //camera.setDisplayOrientation(90);
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera = Camera.open();
        camera.setDisplayOrientation(rotate);
        //Parameters p= camera.getParameters();
        //p.setRotation(90);
        /*if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {   
            p.set("orientation", "portrait");
            p.set("rotation",90);
            Toast.makeText(getApplicationContext(), "Potrait Mode", Toast.LENGTH_SHORT).show();
        }
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {                               
            p.set("orientation", "landscape");          
            p.set("rotation", 90);
            Toast.makeText(getApplicationContext(), "Landscape Mode", Toast.LENGTH_SHORT).show();
        }*/




    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;

    }

0 个答案:

没有答案