如何使用坐标捕获android中的图像

时间:2013-07-15 07:06:50

标签: java android image-processing bitmap image-manipulation

我是Android新手,我想把问题摆在你面前,

我想捕获四个坐标之间的图像,如下所示。

enter image description here

首先,我将图像转换为位图,然后将其设置为相对布局的背景。我知道这四个坐标。

然后,我怎样才能获得框内的图像并将其设置为另一个布局作为背景。

伙计们,请让我摆脱这种逻辑......

3 个答案:

答案 0 :(得分:6)

最后我找到了解决问题的方法,并且我将与你分享,

首先,这是基于+12​​水平的转换理论完成的。  好的,我的问题在“OpenCv for Android”

的帮助下得到了解决

这是代码..

public class MainActivity extends Activity implements CvCameraViewListener2,OnTouchListener 
{

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

};

@Override
protected void onCreate(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
                }
             Mat inputMat = new Mat();
             Mat outputMat = new Mat();
             descBitmap=sourceBitmap;
             Utils.bitmapToMat(sourceBitmap, inputMat);
                List<Point> src_pnt = new ArrayList<Point>();
                Point p0 = new Point(0, 0);
                src_pnt.add(p0);
                Point p1 = new Point(10, 100);
                src_pnt.add(p1);
                Point p2 = new Point(100, 125);
                src_pnt.add(p2);
                Point p3 = new Point(90, 20);                   
                src_pnt.add(p3);
                Mat startM = Converters.vector_Point2f_to_Mat(src_pnt);

                List<Point> dst_pnt = new ArrayList<Point>();
                Point p4 = new Point(0.0, 0.0);
                dst_pnt.add(p4);
                Point p5 = new Point(0.0, sourceBitmap.getHeight());
                dst_pnt.add(p5);
                Point p6 = new Point(sourceBitmap.getWidth(), sourceBitmap.getHeight());
                dst_pnt.add(p6);
                Point p7 = new Point(sourceBitmap.getWidth(), 0);
                dst_pnt.add(p7);
                Mat endM = Converters.vector_Point2f_to_Mat(dst_pnt);
                Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);
                Size size = new Size(sourceBitmap.getWidth(), sourceBitmap.getHeight());
                Scalar scalar = new Scalar(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 


}

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

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

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

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}


}

如果有人有疑问请将它放在这里..

有一个快乐的编码,拉姆..

答案 1 :(得分:0)

关于在坐标中获取图像

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

这将创建一个像你的后面

的裁剪位图

答案 2 :(得分:0)

这将(最有可能)创建一个4,5,6等点的图像,您可以在ImageView上手动点击以获取图像。这应该适用于超过4个点。

@Override
protected void onCreate(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 = new Canvas(resultingImage);

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    Path path=new Path();

    imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(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(new PorterDuffXfermode(Mode.SRC_IN));
                canvas.drawBitmap(bitmap2, 0, 0, paint);
                compositeImageView.setImageBitmap(resultingImage);
                return true;
            }
        }
    });
}