屏幕旋转时保持图片位置

时间:2016-07-27 18:20:16

标签: android android-camera

屏幕旋转时如何保持图片位置。使用此代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


import android.hardware.Camera.Face;
import android.hardware.Camera.FaceDetectionListener;

public class PreviewActivity extends Activity implements SurfaceHolder.Callback, Camera.ShutterCallback, Camera.PictureCallback {

   Camera mCamera;
   SurfaceView surfaceView;
   Button prompt;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      surfaceView = (SurfaceView) findViewById(R.id.preview);
      surfaceView.getHolder().addCallback(this);
      surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


   }

   @Override
   public void onPause() {
      super.onPause();
      Toast.makeText(this, "onPause!", Toast.LENGTH_SHORT).show();

   }

   @Override
   public void onDestroy() {
      super.onDestroy();
     Toast.makeText(this, "onDestroy!", Toast.LENGTH_SHORT).show();
   }

   public void onCancelClick(View v) {
      finish();
   }

   public void onSnapClick(View v) {
      mCamera.takePicture(this, null, null, this); // Invoke onShutter()
   }

   @Override
   public void onShutter() {
      Toast.makeText(this, "onShutter!", Toast.LENGTH_SHORT).show();
   }

   @Override
   public void onPictureTaken(byte[] data, Camera camera) {
      Toast.makeText(this, "onPictureTaken!", Toast.LENGTH_SHORT).show();
      if (data != null) {
         Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

         if (bitmap != null) {

            File file = new File(Environment.getExternalStorageDirectory() + "/dirr");
            if (!file.isDirectory()) {
               file.mkdir();
            }

            file = new File(Environment.getExternalStorageDirectory() + "/dirr", System.currentTimeMillis() + ".jpg");


            try {
               FileOutputStream fileOutputStream = new FileOutputStream(file);
               bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);

               fileOutputStream.flush();
               fileOutputStream.close();
            } catch (IOException e) {
               e.printStackTrace();
            } catch (Exception exception) {
               exception.printStackTrace();
            }

         }
         camera.startPreview();//restart

      }
   }

   @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

      Toast.makeText(this, "surfaceChanged!", Toast.LENGTH_SHORT).show();

      Camera.Parameters params = mCamera.getParameters();
      List<Camera.Size> sizes = params.getSupportedPreviewSizes();
      Camera.Size selected = sizes.get(0);

      params.setPreviewSize(selected.width, selected.height);
      //Toast.makeText(this, "size!" + String.valueOf(selected.width + "," + selected.height), Toast.LENGTH_SHORT).show();
      mCamera.setParameters(params);

      mCamera.setDisplayOrientation(90);

      mCamera.startPreview();

   }

   @Override
   public void surfaceCreated(SurfaceHolder holder) {
      Toast.makeText(this, "surfaceCreated!", Toast.LENGTH_SHORT).show();
      mCamera = Camera.open();
      try {

         mCamera.setPreviewDisplay(surfaceView.getHolder());

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

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {


      if (mCamera != null) {
         mCamera.stopPreview();
         mCamera.release();
         mCamera = null;
      }
      Toast.makeText(this, "surfaceDestroyed!", Toast.LENGTH_SHORT).show();
      Log.i("PREVIEW", "surfaceDestroyed");
   }
}

垂直,它会产生:

enter image description here

水平,它会产生:

enter image description here

我想要实现的目标

enter image description here

我认为,因为当旋转屏幕时, mCamera.setDisplayOrientation(90); 会重新启动。

1 个答案:

答案 0 :(得分:1)

您需要检查设备的方向,无论是LANDSCAPE还是PORTRAIT,并根据它设置mCamera.setDisplayOrientation(角度)。你可以这样做:

if (getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT) mCamera.setDisplayOrientation(angle1);
else mCamera.setDisplayOrientation(angle2);