繁体   English   中英

人像模式下的相机预览

[英]Camera preview in portrait mode

我正在创建一个使用相机的应用程序。 相机在横向和纵向都可以完美工作。 但是,结果图像是在横向模式下的帧中完美设置的。 但是在纵向模式下,结果图像以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);

}

请尝试以下代码,这对于您处理图像旋转很有帮助。

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" />

我遇到了同样的问题。 在Android API 13之前,从相机检索的图像与预览图像相同,但是在API 13之后,它们已将预览的Y轴反转。 从相机的角度来看,它是“真实”视图。 将此功能应用于您的位图:

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

例如,这是用于处理图像或将其保存到sdcard。 但是,如果预览中发生旋转问题,请在“ surfaceChanged”方法中使用此旋转:

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)上使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM