将相机方向设置为纵向无效

时间:2017-02-14 10:57:02

标签: android

我正在使用此代码(如下所示)拍摄按钮点击图片,图片拍摄成功但是处于横向模式,我使用params.set("orientation", "portrait");将相机方向设置为肖像,但这不起作用。我也试过camera.setDisplayOrientation(90);但结果没有变化。那么请帮助我如何以肖像模式拍照?

我的代码:

package com.example.appdeveloperahmed.ct2;

import android.hardware.Camera;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


@SuppressWarnings("deprecation")
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = (Button) findViewById(R.id.button);
        b.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        takeSnapShots();
    }


    private void takeSnapShots() {

        SurfaceView surface = new SurfaceView(this);
        Camera camera = null;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        int cameraCount = Camera.getNumberOfCameras();

        for (int camIdx=0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
                camera = Camera.open(camIdx);
        }
        if(camera==null)
            camera = Camera.open();

        try {
            camera.setPreviewDisplay(surface.getHolder());
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        Camera.Parameters parameters = camera.getParameters();
        parameters.set("orientation", "portrait");
        camera.setParameters(parameters);

        camera.startPreview();

        camera.takePicture(null, null, jpegCallback);
    }


    Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;
            try {
                String dir_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
                outStream = new FileOutputStream(dir_path+ File.separator+"sl_img"+"1"+".jpg");
                outStream.write(data);
                outStream.close();
            }
            catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                camera.stopPreview();
                camera.release();
                camera = null;
                Toast.makeText(getApplicationContext(), "Image snapshot Done",Toast.LENGTH_LONG).show();
            }
        }
    };

}

1 个答案:

答案 0 :(得分:0)

我建议你在手机中使用默认的相机选项,而不是在surfaceview中设置相机。请在下面找到我的代码。

 private void callCamera() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAMERA_REQUEST);
}

private Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

private static File getOutputMediaFile(int type) {

    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }
    Random gen = new Random();
    int n = 10000;
    n = gen.nextInt(n);

    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + n + ".jpg");
    } else {
        return null;
    }
    return mediaFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent        data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
 if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {

            pickFromCamera();
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, Message.userCancel, Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(this, "Failed to Capture Image", Toast.LENGTH_SHORT).show();
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

private void pickFromCamera() {
    String path = fileUri.getPath();
     //Use this fileUri 
}

如果要获取捕获图像的大小。

    private void getIMGSize(String path){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;

    Log.e("SizeofImage",String.valueOf(imageWidth)+" "+String.valueOf(imageHeight));

}