不正确拍照自定义相机活动

时间:2014-03-24 13:30:18

标签: android camera orientation

我使用自定义相机。但我打开相机活动但不采用纵向模式。我的代码如下。enter image description here

我使用了按钮点击:

btnTakePicture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent cameraAct = new Intent(getActivity(),
                        CameraActivity.class);
                startActivityForResult(cameraAct, 1);
            }
        });

CameraActivity :::

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

onActivity导致imageview设置图片但未设置纵向模式。和以下代码::

@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.setRotation(90);
                /*imgStorePicture.setScaleType(ScaleType.FIT_XY);*/

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





            }
        }
    }

0 个答案:

没有答案