使用ImageView的onActivityResult

时间:2017-02-15 05:01:02

标签: java android imageview

我有一种拍摄照片的方法,根据我的需要拍摄垂直形状的样本,横向拍摄并保存垂直照片,就像我希望进行锻炼练习一样,Activity称为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();
}

4 个答案:

答案 0 :(得分:1)

使用RotateLayout,完整的课程和样本可在以下链接中找到。

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

与使用RotateLayout制作新的轮播Bitmap相比,使用Matrix相当容易,内存消耗也更少。

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的{​​{1}}值设置为ExifInterface中的angle属性。RotateLayout已旋转,您无需担心位图或ImageView ExifInterface的任何其他内容。

您可以将角度值从java代码动态设置为orientation对象,如下所示

RotateLayout

答案 1 :(得分:0)

试试这个:

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是图像文件的名称。

答案 2 :(得分:0)

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

中获取imagepath时尝试此操作

答案 3 :(得分:0)

试试吧

function myhandler(event) {
    function(x, y); // Still not a valid name
}
div.onclick = myhandler;