为什么要在横向模式下保存图像?

时间:2015-12-09 11:22:23

标签: android android-camera

在我的代码中,一切进展顺利,但点击了Image在给定目录中以横向模式保存。我尽我所能。请帮助并建议我解决此问题。

完整的代码在下面的打印屏幕中给出。 或者从这里下载

https://www.dropbox.com/sh/vp6ohly4zb5vmvq/AACOyS8PvsRxFGBP9zjlVWhza?dl=0

保存在SD卡中的图像甚至以纵向拍摄,以横向显示

enter image description here

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{

    Camera camera;

    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;
    LayoutInflater controlInflater = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //camera.enableShutterSound(true);


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

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.control, null);
        LayoutParams layoutParamsControl
                = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);

        Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
        buttonTakePicture.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                camera.takePicture(myShutterCallback,
                        myPictureCallback_RAW, myPictureCallback_JPG);
            }});
    }

    ShutterCallback myShutterCallback = new ShutterCallback(){

        @Override
        public void onShutter() {
            // TODO Auto-generated method stub

        }};

    PictureCallback myPictureCallback_RAW = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub

        }};



    PictureCallback myPictureCallback_JPG = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub

            Bitmap bitmapPicture
                    = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);




            String root = Environment.getExternalStorageDirectory().toString();
            File myDir = new File(root + "/cam_Api");
            myDir.mkdirs();

            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(String.format("/sdcard/cam_Api/cam_Ap1_%d.jpg", System.currentTimeMillis()));

                outStream.write(arg0);
                outStream.close();
            }

            catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            catch (IOException e) {
                e.printStackTrace();
            }

            finally {
            }

            Toast.makeText(getApplicationContext(), "Picture Saved", Toast.LENGTH_LONG).show();
            refreshCamera();

//            File file = new File(Environment.getExternalStorageDirectory(),"/cam_Api/MyPhoto2.jpg");

        }};

    public void refreshCamera() {
        if (surfaceHolder.getSurface() == null) {
            return;
        }

        try {
            camera.stopPreview();
        }

        catch (Exception e) {
        }

        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        }
        catch (Exception e) {
        }
    }

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

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                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

        camera = Camera.open();
        camera.setDisplayOrientation(90);
    }

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

2 个答案:

答案 0 :(得分:0)

好像你在呼唤:

camera.setDisplayOrientation(90);

这可能会导致您在应用程序处于纵向模式时看到的横向图像。

您应该使用:

 int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();

获取显示的旋转,然后设置:

caemra.setDisplayOrientation(rotation);

或者只是调用camera.setDisplayOrientation(0)并在android清单中设置screenOrientation,如下所示:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"

防止方向改变。

答案 1 :(得分:0)

您需要旋转图像然后保存。

 public static Bitmap rotateImage(Bitmap bitmapSrc) {
    Matrix matrix = new Matrix();
    matrix.postRotate(CameraConfigurationUtils.previewRotation);
    return Bitmap.createBitmap(bitmapSrc, 0, 0,
            bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}

获得轮换:

  private static int setCameraDisplayOrientation(Context mContext, android.hardware.Camera.CameraInfo info) {
    int rotation = ((CameraActivity) mContext).getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
    }

    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        previewRotation = (info.orientation + degrees) % 360;
        previewRotation = (360 - previewRotation) % 360;  // compensate the mirror
    } else {  // back-facing
        previewRotation = (info.orientation - degrees + 360) % 360;
    }
  return previewRotation;
  }