【问题标题】:Creating bitmap from area selected by touch, returns wrong image part android从触摸选择的区域创建位图,返回错误的图像部分android
【发布时间】:2019-09-05 14:32:27
【问题描述】:

我使用自定义视图放置在这样的图像视图上

<?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>

当用户选择并拖动时,我在此视图中绘制一个矩形,如下所示

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

当我尝试像这样提取所选部分时,

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 必须小于宽度。我认为它与坐标有关,但我无法弄清楚。

【问题讨论】:

    标签: android imageview android-imageview


    【解决方案1】:

    问题在于图像分辨率和 ImageView 尺寸之间的差异。

    解决办法是: 将位图图像的大小调整为与 ImageView 相同的宽度和高度,它将起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 2013-10-28
      • 2015-01-13
      • 2016-03-12
      相关资源
      最近更新 更多