安卓裁剪坐标

5
我想从图库或相机中裁剪一张图片,并获得原始图片的裁剪坐标X和Y,而不是裁剪后的图片。
我需要这些坐标以便在POST请求中使用它们,发送原始图像和裁剪坐标。
如何实现呢?

1
请附上您裁剪图像的代码。 - vasart
没有代码,因为我不知道怎么写。 - BarkovAndrey
@BarkovAndrey,你有想法如何解决这个问题吗?我也遇到了类似的问题,还在努力想办法解决它。 - San
4个回答

0
你需要使用DragRectView类来读取所有的Touch事件和Motion事件,然后在Canvas中绘制一个矩形。通过onTouchEvent函数,你可以获取X和Y值的坐标。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class DragRectView extends View {

    private Paint mRectPaint;    
    private int mStartX = 0;
    private int mStartY = 0;
    private int mEndX = 0;
    private int mEndY = 0;
    private boolean mDrawRect = false;
    private TextPaint mTextPaint = null;

    private OnUpCallback mCallback = null;

    public interface OnUpCallback {
        void onRectFinished(Rect rect);
    }

    public DragRectView(final Context context) {
        super(context);
        init();
    }

    public DragRectView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DragRectView(final Context context, final AttributeSet attrs,     final int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public void setOnUpCallback(OnUpCallback callback) {
        mCallback = callback;
    }

    private void init() {
        mRectPaint = new Paint();
            mRectPaint.setColor(getContext().getResources().getColor(android.R.color.holo_gr    een_light));
        mRectPaint.setStyle(Paint.Style.STROKE);
        mRectPaint.setStrokeWidth(5);

        mTextPaint = new TextPaint();
            mTextPaint.setColor(getContext().getResources().getColor(android.R.color.holo_gr    een_light));
        mTextPaint.setTextSize(20);
    }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDrawRect = false;
                mStartX = (int) event.getX();
                mStartY = (int) event.getY();
                invalidate();
                break;

            case MotionEvent.ACTION_MOVE:
                final int x = (int) event.getX();
                final int y = (int) event.getY();

                if (!mDrawRect || Math.abs(x - mEndX) > 5 || Math.abs(y -     mEndY) > 5) {
                    mEndX = x;
                    mEndY = y;
                    invalidate();
                }

                mDrawRect = true;
                break;

            case MotionEvent.ACTION_UP:
                if (mCallback != null) {
                    mCallback.onRectFinished(new Rect(Math.min(mStartX,     mEndX), Math.min(mStartY, mEndY),
                            Math.max(mEndX, mStartX), Math.max(mEndY,     mStartX)));
                }
                invalidate();
                break;

            default:
                break;
        }

        return true;
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);

        if (mDrawRect) {
            canvas.drawRect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
                    Math.max(mEndX, mStartX), Math.max(mEndY, mStartY), mRectPaint);
            canvas.drawText("  (" + Math.abs(mStartX - mEndX) + ", " + Math.abs(mStartY - mEndY) + ")",
                    Math.max(mEndX, mStartX), Math.max(mEndY, mStartY),     mTextPaint);
        }
    }
}

这将是主方法:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    Bitmap Imag;
    ImageView view;
    TextView text;
    int picw, pich;
    int pix[];
    int x1, x2, y1, y2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final DragRectView view2 = (DragRectView)     findViewById(R.id.dragRect);
        Imag = BitmapFactory.decodeResource(getResources(), R.drawable.rgb);
        picw = Imag.getWidth();
        pich = Imag.getHeight();
        view = (ImageView) findViewById(R.id.image);
        view.setImageBitmap(Imag);
        text = (TextView) findViewById(R.id.tex);
        pix = new int[picw * pich];

        if (null != view2) {
            view2.setOnUpCallback(new DragRectView.OnUpCallback() {
                @Override
                public void onRectFinished(final Rect rect) {
                    x1 = rect.left;
                    y1 = rect.top;
                    x2 = rect.right;
                    y2 = rect.bottom;

                    Toast.makeText(getApplicationContext(), "Coordenadas (" + x1 + ", " + y1 + ", " + x2 + ", " + y2 + ")", Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}    

0
我使用https://github.com/Yalantis/uCrop库的com.github.yalantis:ucrop:2.2.6-native版本来获取裁剪图像边界,并对其进行了一些修改。
open class UCropActivity1 : BaseActivity() {
companion object {
    const val DEFAULT_COMPRESS_QUALITY = 90
    val DEFAULT_COMPRESS_FORMAT = CompressFormat.JPEG
    const val NONE = 0
    const val SCALE = 1
    const val ROTATE = 2
    const val ALL = 3
    private const val TABS_COUNT = 3

    init {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
    }
}

@ColorInt
private var mRootViewBackgroundColor = 0

@DrawableRes
private var mToolbarCropDrawable = 0
private var mGestureCropImageView: GestureCropImageView? = null
private var mOverlayView: OverlayView? = null
private var mWrapperStateAspectRatio: ViewGroup? = null
private var mBlockingView: View? = null
private var type: String? = null
private var mCompressFormat = DEFAULT_COMPRESS_FORMAT
private var mCompressQuality = DEFAULT_COMPRESS_QUALITY
private var mAllowedGestures = intArrayOf(SCALE, ROTATE, ALL)

private val mImageListener: TransformImageListener = object : TransformImageListener {
    override fun onRotate(currentAngle: Float) {
    }

    override fun onScale(currentScale: Float) {
    }

    override fun onLoadComplete() {
        ucrop?.animate()?.alpha(1f)?.setDuration(300)?.interpolator = AccelerateInterpolator()
        mBlockingView?.isClickable = false
    }

    override fun onLoadFailure(e: Exception) {
        setResultError(e)
        finish()
    }
}

override fun getLayoutId(): Int? {
    return R.layout.activity_crop
}

public override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val intent = intent
    setupViews(intent)
    setImageData(intent)
}

override fun onStop() {
    super.onStop()
    if (mGestureCropImageView != null) {
        mGestureCropImageView?.cancelAllAnimations()
    }
}

/**
 * This method extracts all data from the incoming intent and setups views properly.
 */
private fun setImageData(intent: Intent) {
    val inputUri = intent.getParcelableExtra<Uri>(UCrop1.EXTRA_INPUT_URI)
    val outputUri = intent.getParcelableExtra<Uri>(UCrop1.EXTRA_OUTPUT_URI)
    type = intent.getStringExtra(UCrop1.EXTRA_TYPE)
    processOptions(intent)
    if (inputUri != null && outputUri != null) {
        try {
            mGestureCropImageView?.setImageUri(inputUri, outputUri)
        } catch (e: Exception) {
            setResultError(e)
            finish()
        }
    } else {
        setResultError(NullPointerException(getString(com.yalantis.ucrop.R.string.ucrop_error_input_data_is_absent)))
        finish()
    }
}

/**
 * This method extracts [#optionsBundle][UCrop1.Options] from incoming intent
 * and setups Activity, [OverlayView] and [CropImageView] properly.
 */
private fun processOptions(intent: Intent) {
    // Bitmap compression options
    val compressionFormatName = intent.getStringExtra(UCrop1.Options.EXTRA_COMPRESSION_FORMAT_NAME)
    var compressFormat: CompressFormat? = null
    if (!TextUtils.isEmpty(compressionFormatName)) {
        compressFormat = CompressFormat.valueOf(compressionFormatName!!)
    }
    mCompressFormat = compressFormat ?: DEFAULT_COMPRESS_FORMAT
    mCompressQuality = intent.getIntExtra(UCrop1.Options.EXTRA_COMPRESSION_QUALITY, DEFAULT_COMPRESS_QUALITY)

    // Gestures options
    val allowedGestures = intent.getIntArrayExtra(UCrop1.Options.EXTRA_ALLOWED_GESTURES)
    if (allowedGestures != null && allowedGestures.size == TABS_COUNT) {
        mAllowedGestures = allowedGestures
    }

    // Crop image view options
    mGestureCropImageView?.maxBitmapSize = intent.getIntExtra(UCrop1.Options.EXTRA_MAX_BITMAP_SIZE, CropImageView.DEFAULT_MAX_BITMAP_SIZE)
    mGestureCropImageView?.setMaxScaleMultiplier(intent.getFloatExtra(UCrop1.Options.EXTRA_MAX_SCALE_MULTIPLIER, CropImageView.DEFAULT_MAX_SCALE_MULTIPLIER))
    mGestureCropImageView?.setImageToWrapCropBoundsAnimDuration(intent.getIntExtra(UCrop1.Options.EXTRA_IMAGE_TO_CROP_BOUNDS_ANIM_DURATION, CropImageView.DEFAULT_IMAGE_TO_CROP_BOUNDS_ANIM_DURATION).toLong())

    // Overlay view options
    //mOverlayView?.setFreestyleCropEnabled(intent.getBooleanExtra(UCrop1.Options.EXTRA_FREE_STYLE_CROP, OverlayView.DEFAULT_FREESTYLE_CROP_MODE != OverlayView.FREESTYLE_CROP_MODE_ENABLE));
    mOverlayView?.freestyleCropMode = FREESTYLE_CROP_MODE_ENABLE
    mOverlayView?.setDimmedColor(intent.getIntExtra(UCrop1.Options.EXTRA_DIMMED_LAYER_COLOR, ContextCompat.getColor(this, com.yalantis.ucrop.R.color.ucrop_color_default_dimmed)))
    mOverlayView?.setCircleDimmedLayer(intent.getBooleanExtra(UCrop1.Options.EXTRA_CIRCLE_DIMMED_LAYER, OverlayView.DEFAULT_CIRCLE_DIMMED_LAYER))
    mOverlayView?.setShowCropFrame(intent.getBooleanExtra(UCrop1.Options.EXTRA_SHOW_CROP_FRAME, OverlayView.DEFAULT_SHOW_CROP_FRAME))
    mOverlayView?.setCropFrameColor(intent.getIntExtra(UCrop1.Options.EXTRA_CROP_FRAME_COLOR, ContextCompat.getColor(this, com.yalantis.ucrop.R.color.ucrop_color_default_crop_frame)))
    mOverlayView?.setCropFrameStrokeWidth(intent.getIntExtra(UCrop1.Options.EXTRA_CROP_FRAME_STROKE_WIDTH, resources.getDimensionPixelSize(com.yalantis.ucrop.R.dimen.ucrop_default_crop_frame_stoke_width)))
    mOverlayView?.setShowCropGrid(intent.getBooleanExtra(UCrop1.Options.EXTRA_SHOW_CROP_GRID, OverlayView.DEFAULT_SHOW_CROP_GRID))
    mOverlayView?.setCropGridRowCount(intent.getIntExtra(UCrop1.Options.EXTRA_CROP_GRID_ROW_COUNT, OverlayView.DEFAULT_CROP_GRID_ROW_COUNT))
    mOverlayView?.setCropGridColumnCount(intent.getIntExtra(UCrop1.Options.EXTRA_CROP_GRID_COLUMN_COUNT, OverlayView.DEFAULT_CROP_GRID_COLUMN_COUNT))
    mOverlayView?.setCropGridColor(intent.getIntExtra(UCrop1.Options.EXTRA_CROP_GRID_COLOR, ContextCompat.getColor(this, com.yalantis.ucrop.R.color.ucrop_color_default_crop_grid)))
    mOverlayView?.setCropGridCornerColor(intent.getIntExtra(UCrop1.Options.EXTRA_CROP_GRID_CORNER_COLOR, ContextCompat.getColor(this, com.yalantis.ucrop.R.color.ucrop_color_default_crop_grid)))
    mOverlayView?.setCropGridStrokeWidth(intent.getIntExtra(UCrop1.Options.EXTRA_CROP_GRID_STROKE_WIDTH, resources.getDimensionPixelSize(com.yalantis.ucrop.R.dimen.ucrop_default_crop_grid_stoke_width)))

    mRootViewBackgroundColor = intent.getIntExtra(UCrop1.Options.EXTRA_UCROP_ROOT_VIEW_BACKGROUND_COLOR, ContextCompat.getColor(this, com.yalantis.ucrop.R.color.ucrop_color_crop_background))
    ucrop_frame?.setBackgroundColor(mRootViewBackgroundColor)

    // Aspect ratio options
    val aspectRatioX = intent.getFloatExtra(UCrop1.EXTRA_ASPECT_RATIO_X, 0f)
    val aspectRatioY = intent.getFloatExtra(UCrop1.EXTRA_ASPECT_RATIO_Y, 0f)
    if (aspectRatioX > 0 && aspectRatioY > 0) {
        if (mWrapperStateAspectRatio != null) {
            mWrapperStateAspectRatio!!.visibility = View.GONE
        }
        mGestureCropImageView?.targetAspectRatio = aspectRatioX / aspectRatioY
    } else {
        mGestureCropImageView?.targetAspectRatio = CropImageView.SOURCE_IMAGE_ASPECT_RATIO
    }

    // Result bitmap max size options
    val maxSizeX = intent.getIntExtra(UCrop1.EXTRA_MAX_SIZE_X, 0)
    val maxSizeY = intent.getIntExtra(UCrop1.EXTRA_MAX_SIZE_Y, 0)
    if (maxSizeX > 0 && maxSizeY > 0) {
        mGestureCropImageView?.setMaxResultImageSizeX(maxSizeX)
        mGestureCropImageView?.setMaxResultImageSizeY(maxSizeY)
    }
}

private fun setupViews(intent: Intent) {
    getAppComponent().inject(this)
    mToolbarCropDrawable = intent.getIntExtra(UCrop1.Options.EXTRA_UCROP_WIDGET_CROP_DRAWABLE, com.yalantis.ucrop.R.drawable.ucrop_ic_done)
    initiateRootViews()
    setupControllerClickListeners()
    cropViewScreen(appPref)
}

/**
 * method for controller click listeners
 */
private fun setupControllerClickListeners() {
    ivCancel?.setOnClickListener { showDialogForDiscardChanges() }
    ivConfirm?.setOnClickListener {
        mGestureCropImageView?.cropAndSaveImage(mCompressFormat, mCompressQuality, object : BitmapCropCallback {
            override fun onBitmapCropped(resultUri: Uri, offsetX: Int, offsetY: Int, imageWidth: Int, imageHeight: Int) {
                setResultUri(resultUri, mGestureCropImageView?.targetAspectRatio!!, offsetX, offsetY, imageWidth, imageHeight)
                finish()
            }

            override fun onCropFailure(t: Throwable) {
                setResultError(t)
                finish()
            }
        })
    }
    ivBackDetail?.setOnClickListener { onBackPressed() }
    ivRotate?.setOnClickListener { rotateByAngle() }
}


private fun initiateRootViews() {
    mGestureCropImageView = ucrop?.cropImageView
    mOverlayView = ucrop?.overlayView
    mGestureCropImageView?.setTransformImageListener(mImageListener)
}

private fun rotateByAngle() {
    mGestureCropImageView?.postRotate(90F)
    mGestureCropImageView?.setImageToWrapCropBounds()
}

private fun setResultUri(uri: Uri?, resultAspectRatio: Float, offsetX: Int, offsetY: Int, imageWidth: Int, imageHeight: Int) {
    setResult(RESULT_OK, Intent()
        .putExtra(UCrop1.EXTRA_OUTPUT_URI, uri)
        .putExtra(UCrop1.EXTRA_TYPE, type)
        .putExtra(UCrop1.EXTRA_OUTPUT_CROP_ASPECT_RATIO, resultAspectRatio)
        .putExtra(UCrop1.EXTRA_OUTPUT_IMAGE_WIDTH, imageWidth)
        .putExtra(UCrop1.EXTRA_OUTPUT_IMAGE_HEIGHT, imageHeight)
        .putExtra(UCrop1.EXTRA_OUTPUT_OFFSET_X, offsetX)
        .putExtra(UCrop1.EXTRA_OUTPUT_OFFSET_Y, offsetY)
    )
}

private fun setResultError(throwable: Throwable?) {
    setResult(UCrop1.RESULT_ERROR, Intent().putExtra(UCrop1.EXTRA_ERROR, throwable))
}

}

Ucrop1.kt 文件

class UCrop1 private constructor(source: Uri, destination: Uri, type: String) {
private val mCropIntent: Intent = Intent()
private var mCropOptionsBundle: Bundle = Bundle()

fun withOptions(options: Options): UCrop1 {
    mCropOptionsBundle.putAll(options.optionBundle)
    return this
}

/**
 * Send the crop Intent from an Activity with a custom request code
 *
 * @param activity    Activity to receive result
 * @param requestCode requestCode for result
 */
@JvmOverloads
fun start(activity: Activity, requestCode: Int = REQUEST_CROP) {
    activity.startActivityForResult(getIntent(activity), requestCode)
}

/**
 * Get Intent to start [UCropActivity1]
 *
 * @return Intent for [UCropActivity1]
 */
fun getIntent(context: Context): Intent {
    mCropIntent.setClass(context, UCropActivity1::class.java)
    mCropIntent.putExtras(mCropOptionsBundle)
    return mCropIntent
}

/**
 * Class that helps to setup advanced configs that are not commonly used.
 * Use it with method [.withOptions]
 */
class Options {
    val optionBundle: Bundle = Bundle()

    /**
     * Set one of bitmap that will be used to save resulting Bitmap.
     */
    fun setCompressionFormat(format: CompressFormat) {
        optionBundle.putString(EXTRA_COMPRESSION_FORMAT_NAME, format.name)
    }

    /**
     * Set compression quality [0-100] that will be used to save resulting Bitmap.
     */
    fun setCompressionQuality(@IntRange(from = 0) compressQuality: Int) {
        optionBundle.putInt(EXTRA_COMPRESSION_QUALITY, compressQuality)
    }

    /**
     * @param hide - set to true to hide the bottom controls (shown by default)
     */
    fun setHideBottomControls(hide: Boolean) {
        optionBundle.putBoolean(EXTRA_HIDE_BOTTOM_CONTROLS, hide)
    }

    /**
     * @param enabled - set to true to let user resize crop bounds (disabled by default)
     */
    fun setFreeStyleCropEnabled(enabled: Boolean) {
        optionBundle.putBoolean(EXTRA_FREE_STYLE_CROP, enabled)
    }

    companion object {
        const val EXTRA_COMPRESSION_FORMAT_NAME = "$EXTRA_PREFIX.CompressionFormatName"
        const val EXTRA_COMPRESSION_QUALITY = "$EXTRA_PREFIX.CompressionQuality"
        const val EXTRA_ALLOWED_GESTURES = "$EXTRA_PREFIX.AllowedGestures"
        const val EXTRA_MAX_BITMAP_SIZE = "$EXTRA_PREFIX.MaxBitmapSize"
        const val EXTRA_MAX_SCALE_MULTIPLIER = "$EXTRA_PREFIX.MaxScaleMultiplier"
        const val EXTRA_IMAGE_TO_CROP_BOUNDS_ANIM_DURATION = "$EXTRA_PREFIX.ImageToCropBoundsAnimDuration"
        const val EXTRA_DIMMED_LAYER_COLOR = "$EXTRA_PREFIX.DimmedLayerColor"
        const val EXTRA_CIRCLE_DIMMED_LAYER = "$EXTRA_PREFIX.CircleDimmedLayer"
        const val EXTRA_SHOW_CROP_FRAME = "$EXTRA_PREFIX.ShowCropFrame"
        const val EXTRA_CROP_FRAME_COLOR = "$EXTRA_PREFIX.CropFrameColor"
        const val EXTRA_CROP_FRAME_STROKE_WIDTH = "$EXTRA_PREFIX.CropFrameStrokeWidth"
        const val EXTRA_SHOW_CROP_GRID = "$EXTRA_PREFIX.ShowCropGrid"
        const val EXTRA_CROP_GRID_ROW_COUNT = "$EXTRA_PREFIX.CropGridRowCount"
        const val EXTRA_CROP_GRID_COLUMN_COUNT = "$EXTRA_PREFIX.CropGridColumnCount"
        const val EXTRA_CROP_GRID_COLOR = "$EXTRA_PREFIX.CropGridColor"
        const val EXTRA_CROP_GRID_CORNER_COLOR = "$EXTRA_PREFIX.CropGridCornerColor"
        const val EXTRA_CROP_GRID_STROKE_WIDTH = "$EXTRA_PREFIX.CropGridStrokeWidth"
        const val EXTRA_TOOL_BAR_COLOR = "$EXTRA_PREFIX.ToolbarColor"
        const val EXTRA_STATUS_BAR_COLOR = "$EXTRA_PREFIX.StatusBarColor"
        const val EXTRA_UCROP_COLOR_CONTROLS_WIDGET_ACTIVE = "$EXTRA_PREFIX.UcropColorControlsWidgetActive"
        const val EXTRA_UCROP_WIDGET_COLOR_TOOLBAR = "$EXTRA_PREFIX.UcropToolbarWidgetColor"
        const val EXTRA_UCROP_TITLE_TEXT_TOOLBAR = "$EXTRA_PREFIX.UcropToolbarTitleText"
        const val EXTRA_UCROP_WIDGET_CANCEL_DRAWABLE = "$EXTRA_PREFIX.UcropToolbarCancelDrawable"
        const val EXTRA_UCROP_WIDGET_CROP_DRAWABLE = "$EXTRA_PREFIX.UcropToolbarCropDrawable"
        const val EXTRA_UCROP_LOGO_COLOR = "$EXTRA_PREFIX.UcropLogoColor"
        const val EXTRA_HIDE_BOTTOM_CONTROLS = "$EXTRA_PREFIX.HideBottomControls"
        const val EXTRA_FREE_STYLE_CROP = "$EXTRA_PREFIX.FreeStyleCrop"
        const val EXTRA_ASPECT_RATIO_SELECTED_BY_DEFAULT = "$EXTRA_PREFIX.AspectRatioSelectedByDefault"
        const val EXTRA_ASPECT_RATIO_OPTIONS = "$EXTRA_PREFIX.AspectRatioOptions"
        const val EXTRA_UCROP_ROOT_VIEW_BACKGROUND_COLOR = "$EXTRA_PREFIX.UcropRootViewBackgroundColor"
    }

}

companion object {
    const val REQUEST_CROP = 69
    const val RESULT_ERROR = 96
    const val MIN_SIZE = 10
    private const val EXTRA_PREFIX = "APPLICATION_ID"
    const val EXTRA_INPUT_URI = "$EXTRA_PREFIX.InputUri"
    const val EXTRA_OUTPUT_URI = "$EXTRA_PREFIX.OutputUri"
    const val EXTRA_TYPE = "$EXTRA_PREFIX.Type"
    const val EXTRA_OUTPUT_CROP_ASPECT_RATIO = "$EXTRA_PREFIX.CropAspectRatio"
    const val EXTRA_OUTPUT_IMAGE_WIDTH = "$EXTRA_PREFIX.ImageWidth"
    const val EXTRA_OUTPUT_IMAGE_HEIGHT = "$EXTRA_PREFIX.ImageHeight"
    const val EXTRA_OUTPUT_OFFSET_X = "$EXTRA_PREFIX.OffsetX"
    const val EXTRA_OUTPUT_OFFSET_Y = "$EXTRA_PREFIX.OffsetY"
    const val EXTRA_ERROR = "$EXTRA_PREFIX.Error"
    const val EXTRA_ASPECT_RATIO_X = "$EXTRA_PREFIX.AspectRatioX"
    const val EXTRA_ASPECT_RATIO_Y = "$EXTRA_PREFIX.AspectRatioY"
    const val EXTRA_MAX_SIZE_X = "$EXTRA_PREFIX.MaxSizeX"
    const val EXTRA_MAX_SIZE_Y = "$EXTRA_PREFIX.MaxSizeY"

    /**
     * This method creates new Intent builder and sets both source and destination image URIs.
     *
     * @param source      Uri for image to crop
     * @param destination Uri for saving the cropped image
     * @param type String for the image type
     */
    fun of(source: Uri, destination: Uri, type: String): UCrop1 {
        return UCrop1(source, destination, type)
    }

    /**
     * Retrieve cropped image Uri from the result Intent
     *
     * @param intent crop result intent
     */
    fun getOutput(intent: Intent): Uri? {
        return intent.getParcelableExtra(EXTRA_OUTPUT_URI)
    }

    /**
     * Method retrieves error from the result intent.
     *
     * @param result crop result Intent
     * @return Throwable that could happen while image processing
     */
    fun getError(result: Intent): Throwable? {
        return result.getSerializableExtra(EXTRA_ERROR) as Throwable?
    }
}

init {
    mCropOptionsBundle.putParcelable(EXTRA_INPUT_URI, source)
    mCropOptionsBundle.putParcelable(EXTRA_OUTPUT_URI, destination)
    mCropOptionsBundle.putString(EXTRA_TYPE, type)
}

}

在您的活动或片段中,在onActivityResult方法内编写以下代码:
  val croppedOffsetX: Int = data.extras?.getInt(UCrop1.EXTRA_OUTPUT_OFFSET_X)!!
        val croppedOffsetY: Int = data.extras?.getInt(UCrop1.EXTRA_OUTPUT_OFFSET_Y)!!
        val croppedWidth: Int = data.extras?.getInt(UCrop1.EXTRA_OUTPUT_IMAGE_WIDTH)!!
        val croppedHeight: Int = data.extras?.getInt(UCrop1.EXTRA_OUTPUT_IMAGE_HEIGHT)!!

        croppedImageRect = Rect(croppedOffsetX, croppedOffsetY, croppedWidth, croppedHeight)

0

lockdawn的答案对我有用,但我不得不更正这一行...

Math.max(mEndX, mStartX), Math.max(mEndY,     mStartX)));

我认为这行代码应该是:

Math.max(mEndX, mStartX), Math.max(mEndY,     mStartY)));

0
  File tempDir = Environment.getExternalStorageDirectory();
                    tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
                    tempDir.mkdir();
                    File tempFile = null;
                    try {
                        tempFile = File.createTempFile("Trendii", ".png", tempDir);

                        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                        byte[] bitmapData = bytes.toByteArray();

                        //write the bytes in file
                        FileOutputStream fos = new FileOutputStream(tempFile);
                        fos.write(bitmapData);
                        fos.flush();
                        fos.close();


                        float height = 0, width = 0;

                        if (bitmap != null) {
                            height = bitmap.getHeight();
                            width = bitmap.getWidth();
                        }

                        Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
                        Crop.of(Uri.fromFile(tempFile), destination, inspirationId, height, width).asSquare().start(InspirationDetailsActivity.this);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onPermissionDenied(PermissionDeniedResponse response) {
                    showSettingsDialog();
                }

                @Override
                public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                    token.continuePermissionRequest();
                }
            }).check();
        } else {


            File tempDir = Environment.getExternalStorageDirectory();
            tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
            tempDir.mkdir();
            File tempFile = null;
            try {
                tempFile = File.createTempFile("Trendii", ".png", tempDir);

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                byte[] bitmapData = bytes.toByteArray();

                //write the bytes in file
                FileOutputStream fos = new FileOutputStream(tempFile);
                fos.write(bitmapData);
                fos.flush();
                fos.close();

                float height = 0, width = 0;

                if (bitmap != null) {
                    height = bitmap.getHeight();
                    width = bitmap.getWidth();
                }

                Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
                Crop.of(Uri.fromFile(tempFile), destination, inspirationId, height, width).asSquare().start(this);

            } catch (IOException e) {
                e.printStackTrace();
            }

使用这段代码裁剪图像,但在API级别28和29中无法正常工作,有人能提供任何解决方案吗? - s.j

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接