纵向模式下的相机预览

时间:2015-11-04 08:06:03

标签: android android-layout

我正在创建一个使用相机的应用。相机在风景和肖像方面都非常完美。但结果图像在横向模式的框架中完美设置。但是在纵向上,结果图像设置在90度方向的框架中。如何解决这个问题?

   public void createImageInImageCenter() {



    Bitmap backgroundBitmap = DgCamActivity.photo;



    backgroundBitmap = backgroundBitmap.createScaledBitmap(
            backgroundBitmap, 900, 700, true);


    Bitmap bitmapToDrawInTheCenter = null;
    File f = new File(FrameGridView.selected_img);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    try {
        bitmapToDrawInTheCenter = BitmapFactory.decodeStream(
                new FileInputStream(f), null, options);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    bitmapToDrawInTheCenter = bitmapToDrawInTheCenter.createScaledBitmap(
            bitmapToDrawInTheCenter, 900, 700, true);

    resultBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(),
            backgroundBitmap.getHeight(), backgroundBitmap.getConfig());
    Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(backgroundBitmap, new Matrix(), null);

    canvas.drawBitmap(bitmapToDrawInTheCenter, 0, 0, new Paint());

    ImageView image = (ImageView) findViewById(R.id.final_img);
    image.setImageBitmap(resultBitmap);

}

2 个答案:

答案 0 :(得分:1)

请尝试下面的代码,它有助于您处理图像旋转。

public class CaptureImage extends Activity {

private static final int PICK_CAMERA_IMAGE = 2;

ImageView img;
Button btn;
double d = 1.2;

private Uri mImageCaptureUri;

public static String userPicPath;
File file;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_capture_image);

    img = (ImageView) findViewById(R.id.activity_capture_image_img);
    btn = (Button) findViewById(R.id.button1);

    btn.setText(String.valueOf(d));

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            SimpleDateFormat dateFormatter = new SimpleDateFormat(
                    "yyyyMMdd_HHmmss", Locale.US);

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            file = new File(Environment.getExternalStorageDirectory()
                    + "/MyImage", "img_"
                    + dateFormatter.format(new Date()).toString() + ".png");
            userPicPath = file.getPath();
            mImageCaptureUri = Uri.fromFile(file);

            intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
            startActivityForResult(intent, PICK_CAMERA_IMAGE);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_CAMERA_IMAGE && resultCode == RESULT_OK) {

        Log.d("CaptureImage", mImageCaptureUri.toString());

        Bitmap bitmapProfile = getBitmap(userPicPath, this);

        img.setImageBitmap(rotatedBitmap(file, bitmapProfile));

    }
}

public static Bitmap rotatedBitmap(File imageFile, Bitmap source) {

    try {
        int rotate = 0;
        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        Log.v("Capture Image", "Exif orientation: " + orientation + ":"
                + String.valueOf(rotate));

        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);

        return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, false);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public static Bitmap getBitmap(String path, Context context) {
    Uri uri = Uri.fromFile(new File(path));
    InputStream in = null;
    ContentResolver mContentResolver = context.getContentResolver();
    try {
        // final int IMAGE_MAX_SIZE = 2048;
        final int IMAGE_MAX_SIZE = 1024;
        in = mContentResolver.openInputStream(uri);

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(in, null, o);
        in.close();

        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int) Math.pow(
                    2,
                    (int) Math.round(Math.log(IMAGE_MAX_SIZE
                            / (double) Math.max(o.outHeight, o.outWidth))
                            / Math.log(0.5)));
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        in = mContentResolver.openInputStream(uri);
        Bitmap b = BitmapFactory.decodeStream(in, null, o2);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        b.compress(Bitmap.CompressFormat.JPEG, 25, stream);
        in.close();

        return b;
    } catch (FileNotFoundException e) {
        Log.e("CaptureImage", "file " + path + " not found");
    } catch (IOException e) {
        Log.e("CaptureImage", "file " + path + " not found");
    }
    return null;
}

}

和布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Capture Image" />

<ImageView
    android:id="@+id/activity_capture_image_img"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_launcher" />

答案 1 :(得分:0)

我遇到了同样的问题。在Android API 13之前,来自相机的已恢复图像与预览图像相同,但在API 13之后,它们已将Y轴从预览中反转。它是真实的&#34;从相机的角度来看。将此函数应用于您的位图:

public Bitmap rotateBitmap(Bitmap source) {
  orientation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
  Matrix matrix = new Matrix();

  if(android.os.Build.VERSION.SDK_INT > 13) {
    float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1};
    Matrix matrixMirrorY = new Matrix();
    matrixMirrorY.setValues(mirrorY);
    matrix.postConcat(matrixMirrorY);
  }

  switch (orientation) {
    case Surface.ROTATION_0:
      if(android.os.Build.VERSION.SDK_INT > 13) {
        matrix.postRotate(90);
      } else {
        matrix.postRotate(270);
      }
     break;

     case Surface.ROTATION_90:
       matrix.postRotate(0);
     break;

     case Surface.ROTATION_180:
       if(android.os.Build.VERSION.SDK_INT > 13) {
         matrix.postRotate(270);
       } else {
         matrix.postRotate(90);
       }
       break;

       case Surface.ROTATION_270:
         matrix.postRotate(180);
       break;
  }

  return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

这用于处理图像或将其保存到SD卡中。但是如果预览中出现旋转问题,请在&#34; surfaceChanged&#34;中使用此旋转。方法:

Camera.Parameters parameters = mCamera.getParameters();
orientation = getActivity().getWindowManager().getDefaultDisplay().getRotation();

switch (orientation) {
  case Surface.ROTATION_0:
    mCamera.setDisplayOrientation(90);
  break;

  case Surface.ROTATION_90:
    mCamera.setDisplayOrientation(0);
  break;

  case Surface.ROTATION_180:
    mCamera.setDisplayOrientation(270);
  break;

  case Surface.ROTATION_270:
    mCamera.setDisplayOrientation(180);
  break;
}

测试并使用Android 4.x(AOSP,Cyanogen和FirePhone)。