从触摸选择的区域创建位图,返回错误的图像部分android [英] Creating bitmap from area selected by touch, returns wrong image part android

查看:90
本文介绍了从触摸选择的区域创建位图,返回错误的图像部分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);

        }
    });

我总是得到错误的区域,有时会崩溃,因为x必须小于宽度.我认为这与坐标有关,但我无法弄清楚.

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.

推荐答案

问题出在图像分辨率和ImageView尺寸之间.

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

解决方案是: 将位图图像的大小调整为与ImageView相同的宽度和高度,然后它将起作用.

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

这篇关于从触摸选择的区域创建位图,返回错误的图像部分android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆