Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

imageview - Creating bitmap from area selected by touch, returns wrong image part android

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...