Android库 - android-crop旋转捕获的图像

时间:2015-06-19 15:50:39

标签: android

我使用的是Android-Crop https://github.com/jdamcd/android-crop,以便使用相机onActivityResult捕获图像来平方裁剪结果。问题在于,由于某些原因,这个库接缝无法预测地旋转图像。

以下是创建我正在处理onActivityResult - CropImageActivity的输出的库的Activity。

剪裁的结果只是在ImageView中加载Picasso。图像文件临时存储在 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)中,此文件以EXTRA_OUTPUT的形式提供,用于捕获图像,然后进行裁剪,然后加载到ImageVIew中。

问题在于,对于某些Android手机而言,图像是正确旋转的,但对于其他Android手机则不然。 Nexus 5还可以,Xperia Z不是等等......

我正在寻找一种简单的方法来确保图像正确旋转。 在此先感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

我使用过This cropping library并且它工作正常。以下是将要解决的步骤。

  1. 在build.gradle(app)文件中添加“compile'c​​om.isseiaoki:simplecropview:1.1.7'”

  2. 在主类文件中创建一个全局变量,您可以从中调用相机意图     private static final int REQUEST_CODE_FOR_IMAGE_CROPPING = 65;

    public static final int REQUEST_CODE_FOR_OPENCAMERA = 300;

  3. 打开相机的onButton点击列表编写代码

    // create file 
    File newfile = createFile();
    Uri outputFileUri = Uri.fromFile(newfile);
    String uriString = outputFileUri.toString();
    
    Intent intent = new Intent();
    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    context.startActivityForResult(intent, OPENCAMERA);
    
  4. 在onCreate()之后写下创建文件的方法。

     private File createFile() {
    String root = Environment.getExternalStorageDirectory().toString();
    myDir = new File(root + File.separator + "YourFolderName");
    if (!myDir.exists())
        myDir.mkdirs();
    
    File newfile = new File(myDir, "IMG_" + System.currentTimeMillis() + ".JPG");
    try {
        newfile.createNewFile();
    } catch (IOException e) {
    }
    return newfile;
    }
    
  5. 获取有关相机意图的onActivityResult的数据,如下所示。

    if (requestCode == REQUEST_CODE_FOR_OPENCAMERA) {
    if (resultCode == RESULT_OK) {
            try {
                uriStr = (Uri) data.getExtras().get("data");
    
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uriStr);
    
                if (bitmap != null) {
    
                    Intent gotoCropImage = new Intent(thisActivity, CropMyImageActivity.class);
                    gotoCropImage.setData(uriStr);
                    startActivityForResult(gotoCropImage, REQUEST_CODE_FOR_IMAGE_CROPPING);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled Image capture
            DialogManager.showToast(thisActivity, getString(R.string.user_cancelled_image_capture));
        }
    }
    
  6.   
        
    1. 使用名称CropMyImageActivity.java创建新活动,并在下面编写代码
    2.   
      import android.content.Intent;
      import android.graphics.Bitmap;
      import android.net.Uri;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.ImageButton;
    
      import com.isseiaoki.simplecropview.CropImageView;
      import com.isseiaoki.simplecropview.callback.CropCallback;
      import com.isseiaoki.simplecropview.callback.LoadCallback;
      import com.isseiaoki.simplecropview.callback.SaveCallback;
      import com.pioneer.parivaar.activities.BaseActivity;
    
      public class CropMyImageActivity extends BaseActivity implements View.OnClickListener {
    
    private Uri mSourceUri = null;
    private CropImageView mCropView;
    private Bitmap.CompressFormat mCompressFormat = Bitmap.CompressFormat.JPEG;
    private ImageButton rotateLeft, rotateRight, doneBtn, cancelBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop_my_image);
    
        mCropView = (CropImageView) findViewById(R.id.cropImageView);
        rotateLeft = (ImageButton) findViewById(R.id.buttonRotateLeft);
        rotateRight = (ImageButton) findViewById(R.id.buttonRotateRight);
        doneBtn = (ImageButton) findViewById(R.id.buttonDone);
        cancelBtn = (ImageButton) findViewById(R.id.buttonCancel);
    
        mSourceUri = getIntent().getData();
    
        rotateLeft.setOnClickListener(this);
        rotateRight.setOnClickListener(this);
        doneBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        // load image
        mCropView.setCropMode(CropImageView.CropMode.SQUARE);
        mCropView.load(mSourceUri)
                .useThumbnail(true)
                .execute(mLoadCallback);
    
    
    }
    
    private final LoadCallback mLoadCallback = new LoadCallback() {
        @Override public void onSuccess() {
        }
    
        @Override public void onError(Throwable e) {
        }
    };
    
    private final CropCallback mCropCallback = new CropCallback() {
        @Override public void onSuccess(Bitmap cropped) {
            mCropView.save(cropped)
                    .compressFormat(mCompressFormat)
                    .execute(mSourceUri, mSaveCallback);
        }
    
        @Override public void onError(Throwable e) {
        }
    };
    private final SaveCallback mSaveCallback = new SaveCallback() {
        @Override public void onSuccess(Uri outputUri) {
            Intent i = new Intent();
            i.setData(outputUri);
            setResult(RESULT_OK, i);
            finish();
        }
    
        @Override public void onError(Throwable e) {
        }
    };
    
    
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.buttonRotateLeft:
                mCropView.rotateImage(CropImageView.RotateDegrees.ROTATE_M90D);
                break;
            case R.id.buttonRotateRight:
                mCropView.rotateImage(CropImageView.RotateDegrees.ROTATE_90D);
                break;
            case R.id.buttonDone:
                mCropView.crop(mSourceUri).execute(mCropCallback);
                break;
    
            case R.id.buttonCancel:
                setResult(RESULT_CANCELED);
                finish();
                break;
        }
    }
    }
    
      
        
    1. 创建一个名为activity_crop_my_image.xml的xml文件,如下所示
    2.   
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    
    <com.isseiaoki.simplecropview.CropImageView
        android:id="@+id/cropImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="@dimen/margin25"
        custom:scv_background_color="@color/windowBackground"
        custom:scv_crop_mode="fit_image"
        custom:scv_frame_color="@color/colorAccent"
        custom:scv_frame_stroke_weight="1dp"
        custom:scv_guide_color="@color/colorAccent"
        custom:scv_guide_show_mode="show_always"
        custom:scv_guide_stroke_weight="1dp"
        custom:scv_handle_color="@color/colorAccent"
        custom:scv_handle_show_mode="show_always"
        custom:scv_handle_size="14dp"
        custom:scv_min_frame_size="50dp"
        custom:scv_overlay_color="@color/overlay"
        custom:scv_touch_padding="8dp" />
    
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="@dimen/margin7"
        android:layout_marginRight="@dimen/margin7"
        android:background="@color/black"
        />
    
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/cropImageView"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">
    
        <ImageButton
            android:id="@+id/buttonCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:padding="16dp"
            android:src="@drawable/crop__ic_cancel" />
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:orientation="horizontal">
    
            <ImageButton
                android:id="@+id/buttonRotateLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:padding="@dimen/margin5"
                android:src="@drawable/rotate_left" />
    
            <ImageButton
                android:id="@+id/buttonRotateRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:padding="@dimen/margin5"
                android:src="@drawable/rotate_right" />
    
        </LinearLayout>
    
        <ImageButton
            android:id="@+id/buttonDone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:padding="16dp"
            android:src="@drawable/crop__ic_done" />
    </RelativeLayout>
    

      
        
    1. REQUEST_CODE_FOR_IMAGE_CROPPING的OnActivity结果写下代码
    2.   
    if (requestCode == REQUEST_CODE_FOR_IMAGE_CROPPING ){
            if (resultCode == RESULT_OK ) {
                mExecutor = Executors.newSingleThreadExecutor();
                Uri myUri = data.getData();
                mExecutor.submit(new LoadScaledImageTask(this, myUri, profileImage, calcImageSize()));
                imageTypedFile = new TypedFile("multipart/form-data", new File(myUri.toString().substring(7)));
            } else if (resultCode == RESULT_CANCELED){
                DialogManager.showToast(BigFarmerActivity.this, getString(R.string.user_cancelled_image_cropping));
            }
        }
    

答案 1 :(得分:0)

经过一番努力,我找到了答案。该库正在从MediaStore中提取exif数据,但我没有在那里存储我的临时文件。我想到了两种可能的解决方案。第一种是在裁剪之前读取exif方向,并在裁剪之后设置方向属性。另一种方法是将文件保存到媒体商店。

CropUtil.copyExifRotation(CropUtil.getFromMediaUri(this, this.getContentResolver(), 
this.sourceUri), CropUtil.getFromMediaUri(this, this.getContentResolver(), this.saveUri));
        this.setResultUri(this.saveUri);

答案 2 :(得分:0)

您可以尝试使用此库https://github.com/Yalantis/uCrop 它可以旋转图像。