Skip to content Skip to sidebar Skip to footer

How To Capture An Image In Android With Coordinates

Am new to android , and I would like place my problem in-front of you,, I would like to capture an image between four coordinates , as below.. First of all I convert the image int

Solution 1:

finally i find the solution to my problem and i wana share it with you,

first of all, this is done based on the theory of transformation at +12 level. Ok, My problem is solved with the help of "OpenCv for Android "

this is the code..

publicclassMainActivityextendsActivityimplementsCvCameraViewListener2,OnTouchListener 
{

Bitmap sourceBitmap,descBitmap,sourceBitmap1;
ImageView view,view2;
SurfaceView amSurfaceView ;
Mat mRgba;
  private CameraBridgeViewBase mOpenCvCameraView;     
privateBaseLoaderCallbackmLoaderCallback=newBaseLoaderCallback(this) {
    @OverridepublicvoidonManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i("Yesssssssss", "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }

};

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // initialise bitmap for crop is here
    Bitmap bitmap_source=BitmapFactory.decodeResource(getResources(), R.drawable.quadone);
    if(bitmap_source==null)
        Log.e("bitmap Null","nulllllll");

    // these values should not exceed the limits of bitmap..



    Log.e("Bitmap"," "+bitmap_source.getWidth()+" "+bitmap_source.getHeight());


    sourceBitmap =BitmapFactory.decodeResource(getResources(), R.drawable.quadone);
    sourceBitmap1 =BitmapFactory.decodeResource(getResources(), R.drawable.quadone);
    descBitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    view = (ImageView) findViewById(R.id.imageView1);
    view2=(ImageView) findViewById(R.id.imageView2);

    view.setImageBitmap(sourceBitmap1);
    view.setOnTouchListener(this);
      mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.color_blob_detection_activity_surface_view);
        mOpenCvCameraView.setCvCameraViewListener(this);
    Log.e("MAtt","Startttttttttmmmmmmmmmtttttttt");
    sourceBitmap =bitmap_source;
             if (!OpenCVLoader.initDebug()) {
                    // Handle initialization error
                }
             MatinputMat=newMat();
             MatoutputMat=newMat();
             descBitmap=sourceBitmap;
             Utils.bitmapToMat(sourceBitmap, inputMat);
                List<Point> src_pnt = newArrayList<Point>();
                Pointp0=newPoint(0, 0);
                src_pnt.add(p0);
                Pointp1=newPoint(10, 100);
                src_pnt.add(p1);
                Pointp2=newPoint(100, 125);
                src_pnt.add(p2);
                Pointp3=newPoint(90, 20);                   
                src_pnt.add(p3);
                MatstartM= Converters.vector_Point2f_to_Mat(src_pnt);

                List<Point> dst_pnt = newArrayList<Point>();
                Pointp4=newPoint(0.0, 0.0);
                dst_pnt.add(p4);
                Pointp5=newPoint(0.0, sourceBitmap.getHeight());
                dst_pnt.add(p5);
                Pointp6=newPoint(sourceBitmap.getWidth(), sourceBitmap.getHeight());
                dst_pnt.add(p6);
                Pointp7=newPoint(sourceBitmap.getWidth(), 0);
                dst_pnt.add(p7);
                MatendM= Converters.vector_Point2f_to_Mat(dst_pnt);
                MatperspectiveTransform= Imgproc.getPerspectiveTransform(startM, endM);
                Sizesize=newSize(sourceBitmap.getWidth(), sourceBitmap.getHeight());
                Scalarscalar=newScalar(50.0);
                Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, size, Imgproc.INTER_LINEAR + Imgproc.CV_WARP_FILL_OUTLIERS, Imgproc.BORDER_DEFAULT, scalar);

                Log.e("1=",""+inputMat.cols()+" "+inputMat.rows());
                Log.e("outmat.."," "+outputMat.cols()+" "+outputMat.rows());
                Utils.matToBitmap(outputMat, descBitmap);
                view2.setImageBitmap(descBitmap);

             // ram@san 


}

@OverridepublicvoidonCameraViewStarted(int width, int height) {
    // TODO Auto-generated method stub
    Log.e("onCameraViewStarted","onCameraViewStarted");
}

@OverridepublicvoidonCameraViewStopped() {
    // TODO Auto-generated method stub
    Log.e("onCameraViewStopped","onCameraViewStopped");
}

@Overridepublic Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        // TODO Auto-generated method stub
mRgba= newMat();
Utils.bitmapToMat(sourceBitmap, mRgba);
Utils.matToBitmap(mRgba, descBitmap);
view2.setImageBitmap(sourceBitmap);
        return mRgba;
}

@OverridepublicbooleanonTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stubreturnfalse;
}


}

if any one have doubts please place it here..

have a happy codding, Ram..

Solution 2:

as for getting the image within the co-ordinates

BitmapcroppedBmp= Bitmap.createBitmap(originalBmp, startx, starty, endx, endy);

this will create a cropped bitmap like your after

Solution 3:

This will (most probably) create an image from 4, 5, 6 etc. points that you click manually on the ImageView to obtain. This should work on more than 4 points selected.

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView imageView = (ImageView) findViewById(R.id.img);
    compositeImageView = (ImageView) findViewById(R.id.imageView);

    Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.drawable_android);
    Bitmap bitmap2=BitmapFactory.decodeResource(getResources(), R.drawable.drawable_android_cr);

    Bitmap resultingImage=Bitmap.createBitmap(320, 480, bitmap1.getConfig());
    Canvas canvas = newCanvas(resultingImage);

    Paint paint = newPaint();
    paint.setAntiAlias(true);
    Path path=newPath();

    imageView.setOnTouchListener(newView.OnTouchListener() {
        @OverridepublicbooleanonTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                // textView.setText("Touch coordinates : " +String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));Log.e("X",String.valueOf(event.getX())+"");
                Log.e("y",String.valueOf(event.getY())+"");
                path.lineTo(String.valueOf(event.getX()), String.valueOf(event.getY()));
            }
            if(/*Touch count == 4 or 5 or 6 etc.*/){
                canvas.drawPath(path, paint);
                paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
                canvas.drawBitmap(bitmap2, 0, 0, paint);
                compositeImageView.setImageBitmap(resultingImage);
                returntrue;
            }
        }
    });
}

Post a Comment for "How To Capture An Image In Android With Coordinates"