繁体   English   中英

带有 ImageView 的 onActivityResult

[英]onActivityResult with ImageView

我有一种拍照的方法,按照我想要的方式拍摄垂直形式的样本,水平圈并保存垂直照片,就像我希望的练习练习一样,名为 ViewerActivity 的活动,它是这里问题开始的地方在 ImageView 中显示照片水平样本的时刻:/他正在阅读并且有类 ExifInterface 允许控制图像的方向,但在显示图像的时刻仍然是水平的

这个班级负责发送照片。

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

这个 ViewerActivity 类显示照片然后发送它

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

使用RotateLayout ,完整的类和示例可在以下链接中找到。

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

与使用Matrix制作新的旋转Bitmap相比,使用RotateLayout非常容易并且内存消耗更少。

将您的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>

并设置orientation值,是通过让ExifInterfaceangle物业RotateLayout 。而ImageView旋转,你不需要担心的位图或其他任何东西ExifInterface orientation

您可以从 java 代码动态设置角度值,到RotateLayout对象中,如下所示

rotateLayout.setAngle(newAngle);

尝试这个:

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

其中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");
}

当您在 onActivityResult 中获取图像路径时尝试此操作

尝试一下

    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