在Android中摄像头方向不正确

时间:2012-03-29 09:43:41

标签: android android-camera

我使用自定义相机和表面视图来捕捉图像,但问题是相机方向不正确,以下是我的代码并抱歉我的英语沟通不好。

public class Camera_Activity extends Activity implements
        SurfaceHolder.Callback {
    private Camera camera = null;
    private SurfaceView cameraSurfaceView = null;
    private SurfaceHolder cameraSurfaceHolder = null;
    private boolean previewing = false;

    private Display display = null;

    private static int wid = 0, hgt = 0;

    private LayoutInflater layoutInflater = null;
    private View cameraViewControl = null;
    private LayoutParams layoutParamsControl = null;

    private Button btnCapture = null;
    ImageView mImgView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        display = getWindowManager().getDefaultDisplay();
        wid = display.getWidth();
        hgt = display.getHeight();

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.camera);

        mImgView1 = (ImageView) findViewById(R.id.mImgView1);

        mImgView1.setImageResource(R.drawable.first_leg);

        cameraSurfaceView = (SurfaceView) findViewById(R.id.cameraSurfaceView);
        cameraSurfaceHolder = cameraSurfaceView.getHolder();
        cameraSurfaceHolder.addCallback(this);
        cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        layoutInflater = LayoutInflater.from(getBaseContext());
        layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT);

        cameraViewControl = layoutInflater.inflate(R.layout.cambutton, null);
        this.addContentView(cameraViewControl, layoutParamsControl);

        cameraViewControl = layoutInflater.inflate(R.layout.vampireimage, null);
        this.addContentView(cameraViewControl, layoutParamsControl);

        btnCapture = (Button) findViewById(R.id.btnCapture);
        btnCapture.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                camera.takePicture(cameraShutterCallback,
                        cameraPictureCallbackRaw, cameraPictureCallbackJpeg);
            }
        });
    }

    ShutterCallback cameraShutterCallback = new ShutterCallback() {
        @Override
        public void onShutter() {
            // TODO Auto-generated method stub
            System.out.println("Hello Shutter");
        }
    };

    PictureCallback cameraPictureCallbackRaw = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
            System.out.println("Hello Picture");
        }
    };

    PictureCallback cameraPictureCallbackJpeg = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
            Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0,
                    data.length);

            wid = cameraBitmap.getWidth();
            hgt = cameraBitmap.getHeight();

            Bitmap newImage = Bitmap.createBitmap(wid, hgt,
                    Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(newImage);

            canvas.drawBitmap(cameraBitmap, 0f, 0f, null);

            Constants.mImageBitmap.clear();
            Constants.mImageBitmap.add(cameraBitmap);

            File storagePath = new File(
                    Environment.getExternalStorageDirectory() + "/Photos/");
            storagePath.mkdirs();

            File myImage = new File(storagePath, "1.jpg");

            try {
                FileOutputStream out = new FileOutputStream(myImage);
                newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                Log.d("In Saving File", e + "");
            } catch (IOException e) {
                Log.d("In Saving File", e + "");
            }

            camera.startPreview();

            newImage.recycle();
            newImage = null;

            // cameraBitmap.recycle();
            // cameraBitmap = null;

            Intent mInPreview = new Intent(Camera_Activity.this,
                    Preview_Activity.class);
            mInPreview.putExtra("Value", "First");
            startActivity(mInPreview);
            finish();
        }
    };

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        if (previewing) {
            camera.stopPreview();
            previewing = false;
            System.out.println("Hello 2");
        }
        try {
            Camera.Parameters parameters = camera.getParameters();
            Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
                    .getDefaultDisplay();

            if (display.getRotation() == Surface.ROTATION_0) {
                parameters.setPreviewSize(height, width);
                camera.setDisplayOrientation(90);
            }

            if (display.getRotation() == Surface.ROTATION_90) {
                parameters.setPreviewSize(width, height);
            }

            if (display.getRotation() == Surface.ROTATION_180) {
                parameters.setPreviewSize(height, width);
            }

            if (display.getRotation() == Surface.ROTATION_270) {
                parameters.setPreviewSize(width, height);
                camera.setDisplayOrientation(180);
            }
            camera.setParameters(parameters);

            camera.setPreviewDisplay(cameraSurfaceHolder);
            camera.startPreview();
            previewing = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        try {
            camera = Camera.open();
        } catch (RuntimeException e) {
            Toast.makeText(
                    getApplicationContext(),
                    "Device camera is not working properly, please try after sometime.",
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        System.out.println("Hello 3");
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("3DSoles");
        alert.setMessage("Do U Want to Exit?");
        alert.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });

        alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("3DSoles");
            alert.setMessage("Do U Want to Exit?");
            alert.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                        }
                    });

            alert.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            finish();
                        }
                    });
        }
        return super.onKeyDown(keyCode, event);
    }
}

1 个答案:

答案 0 :(得分:1)

  1. Camera课程不支持setPreview(height, width)

  2. 请参阅http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29了解选择Camera.setDisplayOrientation()的正式方法

  3. 从API 14(ICS及更高版本)开始,相机应支持setDisplayOrientation()而不支持 中断预览。

  4. 对于较旧的平台,我建议在相机预览处于活动状态时禁用方向更改;重置相机(stopPreview() + startPreview)可能需要很长时间才能使用户体验变得轻松自如。通常,预览活动可以使用固定方向,也可以在软件方向更改时“模拟”以响应加速度计事件。