Why image is being saved in Landscape Mode?

旧街凉风 提交于 2019-12-23 05:12:16

问题


In my code all is going well but clicked Image is being saved in Landscape Mode in given Directory. I have tried my best. Please help and suggest me to resolve this issue.

The complete Code is given below with Print screen. or download from here

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

Image saved in SDcard even captured in portrait it appear in landscape

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

回答1:


It seems you are calling:

camera.setDisplayOrientation(90);

which would probably cause the landscape image you are seeing when your application is in portrait mode.

You should use:

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

to get the rotation of your display and then set:

caemra.setDisplayOrientation(rotation);

or just call camera.setDisplayOrientation(0) and set screenOrientation in your android manifest as so:

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

to prevent the orientation changing.




回答2:


you need to rotate the image and then save it.

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

To get the rotation:

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


来源:https://stackoverflow.com/questions/34177582/why-image-is-being-saved-in-landscape-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!