如何捕捉图片肖像模式设置肖像模式?

时间:2014-03-24 11:24:31

标签: android camera orientation

我有相机活动。我捕捉图片但在相机预览中没有显示图片肖像模式。如何在Android中实现。我的代码如下。

public class CameraActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mCameraPreview;
    protected static final int MEDIA_TYPE_IMAGE = 0;
    static String FilePAth = "";
    Button takePicture;
    static String base64string="";

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

        mCamera = getCameraInstance();
        mCameraPreview = new CameraPreview(CameraActivity.this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mCameraPreview);

        takePicture = (Button) findViewById(R.id.btnTakePicture);
        takePicture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                mCamera.takePicture(null, null, mPicture);

            }
        });
    }

    private Camera getCameraInstance() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }

    private static File getOutputMediaFile() {
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "MyCameraApp");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());

        FilePAth = mediaStorageDir.getPath() + File.separator + "IMG_"
                + timeStamp + ".jpg";

        Log.v("log", " FilePAth " + FilePAth);

        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");

        return mediaFile;
    }

    PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {

                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();

                Intent returnIntent = new Intent();
                returnIntent.putExtra("data", data);
                setResult(RESULT_OK, returnIntent);
                finish();

            } catch (FileNotFoundException e) {

            } catch (IOException e) {
            }
        }
    };

    public void onBackPressed() {
        Intent returnIntent = new Intent();
        returnIntent.putExtra("path", FilePAth);
        setResult(RESULT_OK, returnIntent);
        finish();
    };
}

获取数据:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.v("log", " data --> " + data.getByteArrayExtra("data"));
        if (requestCode == 1) {

            if (data.hasExtra("data")) {

                Log.v("log", " request if ");

                Bitmap b = BitmapFactory.decodeByteArray(
                        data.getByteArrayExtra("data"), 0,
                        data.getByteArrayExtra("data").length);



                imgStorePicture.setImageBitmap(b);
                /*imgStorePicture.setScaleType(ScaleType.FIT_XY);*/

                base64string = Base64.encodeBytes(data
                        .getByteArrayExtra("data"));
                Log.v("log", "base64string " + base64string);





            }
        }
    }

1 个答案:

答案 0 :(得分:0)

简短的回答是,你做不到。图片总是按原样返回。您可以更改预览方向,但不能更改返回到onPictureTaken)的JPEG图像的方向。

但你可以通过Exif标头设置JPEG旋转而不解码它。这是最有效的方法,但有些观看者可能仍会显示旋转的图像。

或者,您可以使用JPEG lossless rotation。 Android端口位于GitHub