Creating bitmap from area selected by touch, retur

2019-09-19 15:34发布

问题:

I use custom view placed over an image view like that

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5"
    tools:context=".TextRecognitionActivity">
    <ImageView
        android:id="@+id/surfaceViewImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        />
    <com.gypsywaiter.app.DragRectView
        android:id="@+id/dragRectView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        />

    <Button
        android:id="@+id/capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:layout_weight="1"
        android:text="Capture"/>
</FrameLayout>

And I draw a rectangle in this view upon user selects and drags as follow

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

    /**
     * Sets callback for up
     *
     * @param callback {@link OnUpCallback}
     */
    public void setOnUpCallback(OnUpCallback callback) {
        mCallback = callback;
    }

    /**
     * Inits internal data
     */
    private void init() {
        mRectPaint = new Paint();
        mRectPaint.setColor(getContext().getResources().getColor(android.R.color.holo_green_light));
        mRectPaint.setStyle(Paint.Style.FILL);
        mRectPaint.setStrokeWidth(5); // TODO: should take from resources
        mRectPaint.setAlpha(60);
        mTextPaint = new TextPaint();
        mTextPaint.setColor(getContext().getResources().getColor(android.R.color.holo_green_light));
        mTextPaint.setTextSize(20);
    }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {

        // TODO: be aware of multi-touches
        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();
                Log.d("logger", x + "//" + y);
                //if (!mDrawRect || Math.abs(x - mEndX) > 5 || Math.abs(y - mEndY) > 5) {
                    mEndX = x;
                    mEndY = y;
                    invalidate();
                //}

                mDrawRect = true;
                break;


            case MotionEvent.ACTION_UP:
                int[] location = new int[2];
                getLocationOnScreen(location);
                invalidate();

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

            default:
                break;
        }

        return true;
    }

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


        canvas.drawRect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
            Math.max(mEndX, mStartX), Math.max(mEndY, mStartY), mRectPaint);
    }

When I try to extract that selected part like that,

dragRectView.setOnUpCallback(new DragRectView.OnUpCallback() {
        @Override
        public void onRectFinished(Rect rect) {
            Bitmap nBitmap =  Bitmap.createBitmap(loadedImage,rect.top, rect.left , rect.bottom, rect.right);

        }
    });

I always get the wrong area and some times crash that x must be less than width. I think it is something related to coordinates but I can't figure it out.

回答1:

The problem was in the difference between image resolution and the ImageView dimensions.

Solution is: Resize the bitmap image to be the same width and height of the ImageView and it will work.