简体   繁体   English

带有 ImageView 的 onActivityResult

[英]onActivityResult with ImageView

I have a method to take a photo, take the sample of the vertical form just as I want, the horizontal lap and save the vertical photograph just as I wish for the exercise exercises, Activity called ViewerActivity and it is here Where the problem begins at the moment of displaying in an ImageView the photograph the horizontal sample: / he is reading and there is the class ExifInterface that allows to control the orientation of the image, but at the moment of displaying the Image is still horizontal我有一种拍照的方法,按照我想要的方式拍摄垂直形式的样本,水平圈并保存垂直照片,就像我希望的练习练习一样,名为 ViewerActivity 的活动,它是这里问题开始的地方在 ImageView 中显示照片水平样本的时刻:/他正在阅读并且有类 ExifInterface 允许控制图像的方向,但在显示图像的时刻仍然是水平的

This class is in charge of sending the photo.这个班级负责发送照片。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        try {
            ExifInterface exifInterface = new ExifInterface(photoFile.getAbsolutePath());
            int valor = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (valor) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    orientation = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    orientation = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    orientation = 270;
                    break;
            }
        } catch (Exception e) {
            Log.d("mensaje", e.toString());
        }
        Intent i = new Intent(this, ViewerActivity.class);
        i.putExtra(EXTRA_PHOTO_PATH, photoFile.getAbsolutePath());
        i.putExtra(EXTRA_PHOTO_ORIENTATION, orientation);
        i.putExtra(EXTRA_PHOTO_EDIT, false);
        startActivityForResult(i, 10);

This ViewerActivity class displays the photo and then sends it这个 ViewerActivity 类显示照片然后发送它

private void sendPicture() {
    Intent intent = new Intent();
    intent.putExtra("path", localPath);
    intent.putExtra("orientation",orientation);
    setResult(Activity.RESULT_OK, intent);
    finish();
}

Use RotateLayout ,complete class and sample is available in below link.使用RotateLayout ,完整的类和示例可在以下链接中找到。

https://github.com/rongi/rotate-layout https://github.com/rongi/rotate-layout

use of RotateLayout is quite easy and less memory consumption compare to make new rotated Bitmap using Matrix .与使用Matrix制作新的旋转Bitmap相比,使用RotateLayout非常容易并且内存消耗更少。

place your ImageView inside RotateLayout like below Code将您的ImageView放在RotateLayout如下面的代码

<com.github.rongi.rotate_layout.layout.RotateLayout
  android:id="@+id/form3_container"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:angle="180">

  <ImageView
   android:id="@+id/imageview"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />
</com.github.rongi.rotate_layout.layout.RotateLayout>

And Set orientation value that is get through ExifInterface into angle property in RotateLayout .And ImageView Rotated and you do not need to worry for bitmap or any other thing for ExifInterface orientation .并设置orientation值,是通过让ExifInterfaceangle物业RotateLayout 。而ImageView旋转,你不需要担心的位图或其他任何东西ExifInterface orientation

You can set Angle value dynamically from java code ,into RotateLayout Object like below您可以从 java 代码动态设置角度值,到RotateLayout对象中,如下所示

rotateLayout.setAngle(newAngle);

Try this:尝试这个:

BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);

BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
ExifInterface exif = new ExifInterface(file);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;

int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;

Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);`

where file is the name of the image file.其中file是图像文件的名称。

try {
File f = new File(imagePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
        ExifInterface.TAG_ORIENTATION,
        ExifInterface.ORIENTATION_NORMAL);

int angle = 0;

if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
    angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
    angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
    angle = 270;
}

Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;

Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
        null, options);
bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
        bmp.getHeight(), mat, true);
ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100,
        outstudentstreamOutputStream);
imageView.setImageBitmap(bitmap);

} catch (IOException e) {
Log.w("TAG", "-- Error in setting image");
} catch (OutOfMemoryError oom) {
Log.w("TAG", "-- OOM Error in setting image");
}

try this when you get the imagepath in onActivityResult当您在 onActivityResult 中获取图像路径时尝试此操作

Try it尝试一下

    public Bitmap rotateImage() {
    Bitmap scaled = null;
    try {
        FileInputStream fis = new FileInputStream(file);
        BitmapFactory.Options bounds = new BitmapFactory.Options();
        bounds.inJustDecodeBounds = true;
        Bitmap bm = BitmapFactory.decodeStream(fis);

        ExifInterface exif = new ExifInterface(file.getAbsolutePath());

        String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
        int rotationAngle = 0;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
            rotationAngle = 90;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
            rotationAngle = 180;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
            rotationAngle = 270;
        Matrix matrix = new Matrix();
        matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        int height = rotatedBitmap.getHeight(), scaledheight, scaledwidth;
        int width = rotatedBitmap.getWidth();
        float aspect;

        if (width < height) {// portrait
            aspect = ((float) height / (float) width);
            scaledwidth = 400;
            scaledheight = (int) (400 * aspect);
        } else {// landscape
            aspect = ((float) width / (float) height);
            scaledheight = 400;
            scaledwidth = (int) (400 * aspect);
        }
        scaled = Bitmap.createScaledBitmap(rotatedBitmap, scaledwidth, scaledheight, false);

        File f = new File(getCacheDir(), "scaledBitmap");
        f.createNewFile();

        Bitmap bitmap = scaled;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0 /* ignored for PNG */, bos);
        byte[] bitmapdata = bos.toByteArray();

        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();

        file = f;

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("photofile io error", "EXif not done");
    }
    return scaled;
}

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

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