在位图中加载的图像上绘制矩形

时间:2017-04-27 12:46:36

标签: android bitmap imageview android-canvas picasso

我正在使用 Picasso 库从网址加载图片。图像加载成功之后,我需要在位图中查看图像并在其上绘制一个矩形。触摸图像后,我的活动崩溃了

我已将我的logcat附加到Logcat

,代码是 MainActivity.java

public class MainActivity extends Activity {
int projectedX;
int projectedY;
int width;
int height;

Button btnLoadImage;
TextView textSource;
ImageView imageResult, imageDrawingPane;

final int RQS_IMAGE1 = 1;

Uri source;
Bitmap bitmapMaster;
Canvas canvasMaster;
Bitmap bitmapDrawingPane;
Canvas canvasDrawingPane;
projectPt startPt;


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

    btnLoadImage = (Button)findViewById(R.id.loadimage);
    textSource = (TextView)findViewById(R.id.sourceuri);
    imageResult = (ImageView)findViewById(R.id.result);
    imageDrawingPane = (ImageView)findViewById(R.id.drawingpane);


    btnLoadImage.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View view) {
         /*   Intent intent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, RQS_IMAGE1);*/

            Picasso.with(getApplicationContext()).load("http://54.179.134.139/FTP/viview360/Images/Place3-2017-04-25-12-30-04.jpg").into(imageResult);
        }});

    imageResult.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            int action = event.getAction();
            int x = (int) event.getX();
            int y = (int) event.getY();
            switch(action){
                case MotionEvent.ACTION_DOWN:
                    textSource.setText("ACTION_DOWN- " + x + " : " + y);
                    startPt = projectXY((ImageView)v, bitmapMaster, x, y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    textSource.setText("ACTION_MOVE- " + x + " : " + y);
                     drawOnRectProjectedBitMap((ImageView)v, bitmapMaster, x, y);

                    break;
                case MotionEvent.ACTION_UP:
                    textSource.setText("ACTION_UP- " + x + " : " + y);
                    drawOnRectProjectedBitMap((ImageView)v, bitmapMaster, x, y);

                    finalizeDrawing();
                    break;
            }

            return true;
        }});

}

class projectPt{
    int x;
    int y;

    projectPt(int tx, int ty){
        x = tx;
        y = ty;
    }
}

private projectPt projectXY(ImageView iv, Bitmap bm, int x, int y){
    if(x<0 || y<0 || x > iv.getWidth() || y > iv.getHeight()){
        //outside ImageView
        return null;
    }else{
         projectedX = (int)((double)x * ((double)bm.getWidth()/(double)iv.getWidth()));
         projectedY = (int)((double)y * ((double)bm.getHeight()/(double)iv.getHeight()));

        return new projectPt(projectedX, projectedY);
    }
}

private void drawOnRectProjectedBitMap(ImageView iv, Bitmap bm, int x, int y){

    if(x<0 || y<0 || x > iv.getWidth() || y > iv.getHeight()){
        //outside ImageView
        return;
    }else{
         projectedX = (int)((double)x * ((double)bm.getWidth()/(double)iv.getWidth()));
         projectedY = (int)((double)y * ((double)bm.getHeight()/(double)iv.getHeight()));

        //clear canvasDrawingPane
        canvasDrawingPane.drawColor(Color.TRANSPARENT, Mode.CLEAR);

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(4);
        Rect rect = new Rect(startPt.x, startPt.y, projectedX, projectedY);
        canvasDrawingPane.drawRect(rect,paint);
        width = rect.width();

        height = rect.height();
        imageDrawingPane.invalidate();

       // textSource.setText(x + ":" + y + "/" + iv.getWidth() + " : " + iv.getHeight() + "\n" +
        //                projectedX + " : " + projectedY + "/" + bm.getWidth() + " : " + bm.getHeight()

        textSource.setText(startPt.x + ":" + startPt.y + "/" + iv.getWidth() + " : " + iv.getHeight() + "\n" +
                width + " : " + height+ "/" + bm.getWidth() + " : " + bm.getHeight()

        );
    }
}

private void finalizeDrawing(){
    canvasMaster.drawBitmap(bitmapDrawingPane, 0, 0, null);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Bitmap tempBitmap;


    if(resultCode == RESULT_OK){
        switch (requestCode){
            case RQS_IMAGE1:
                source = data.getData();
                textSource.setText(source.toString());

                try {

                    tempBitmap = BitmapFactory.decodeStream(
                            getContentResolver().openInputStream(source));

                    Config config;
                    if(tempBitmap.getConfig() != null){
                        config = tempBitmap.getConfig();
                    }else{
                        config = Config.ARGB_8888;
                    }


                    bitmapMaster = Bitmap.createBitmap(
                            tempBitmap.getWidth(),
                            tempBitmap.getHeight(),
                            config);

                    canvasMaster = new Canvas(bitmapMaster);
                    canvasMaster.drawBitmap(tempBitmap, 0, 0, null);

                    imageResult.setImageBitmap(bitmapMaster);



                    bitmapDrawingPane = Bitmap.createBitmap(
                            tempBitmap.getWidth(),
                            tempBitmap.getHeight(),
                            config);
                    canvasDrawingPane = new Canvas(bitmapDrawingPane);
                    imageDrawingPane.setImageBitmap(bitmapDrawingPane);



                }
                catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
        }

    }
}

}

和XML是

<LinearLayout 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"
tools:context=".MainActivity" >



<Button
    android:id="@+id/loadimage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Load Image 1" />

<TextView
    android:id="@+id/sourceuri"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@android:color/background_dark"
        android:scaleType="centerInside" />

    <ImageView
        android:id="@+id/drawingpane"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignBottom="@id/result"
        android:layout_alignLeft="@id/result"
        android:layout_alignRight="@id/result"
        android:layout_alignTop="@id/result"
        android:adjustViewBounds="true"
        android:scaleType="centerInside" />
</RelativeLayout>

0 个答案:

没有答案