android - 纵向模式下的相机预览

标签 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);

}

最佳答案

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

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 - 纵向模式下的相机预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33516644/

相关文章:

android - 如何在 Android 中平滑地实现放大缩小?与带有 RTMP 流的 SurfaceView 相关

android - 如何让布局/ View 显示在 viewflipper 下方

c# - EventSystem.Update() 在 Android 上的第一次屏幕触摸时出现巨大的性能峰值

android - 水平 RecyclerView 高度换行内容不起作用

android - PubNub 映射错误

android - 选择复选框后,所有 ListView 项目复选框均未选中

android - Make Go http.Response 详细说明所有参数

android - 如何在线性布局中居中相对布局

java - Android Studio - 图像展开按钮(并打破重量规则?)

android - 以编程方式应用的 9-patch 挂起应用程序