3

I am trying to find contours from the raw bytes received from onPreviewFrame. These raw bytes received are not rotated when we do setDisplayOrientation(according to the android developer docs). It is becoming difficult to rotate the contours alone. How to rotate these bytes efficiently and then process it ? I am using openCV to find the contours.

 @Override 
    public void onPreviewFrame(byte[] data, Camera camera) {
        previewSize = cameraConfigUtil.cameraInstance.getParameters().getPreviewSize(); 
        Mat srcMat = new Mat(previewSize.height, previewSize.width, CvType.CV_8UC3);
        srcMat.put(0, 0, data);
        rect = ImageCorrection.getLargestContour(data, previewSize.height, previewSize.width);
        android.graphics.Rect rectangle = new android.graphics.Rect();
        rectangle.left = rect.x;
        rectangle.top = rect.y;
        rectangle.right = rect.x + rect.width;
        rectangle.bottom = rect.y + rect.height;
        mOverlay.clear(); 
        BarcodeGraphic graphic = new BarcodeGraphic(mOverlay);
        mOverlay.add(graphic);
        graphic.updateItem(rectangle);
        Imgproc.rectangle(srcMat, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0, 255), 3);
        Utils.matToBitmap(srcMat, bitmap);
    } 

Here rectangle gives the four points of the contour, I want this to be rotated according to the angle set in setDisplayOrientation.

android_eng
  • 1,370
  • 3
  • 17
  • 40

1 Answers1

0

Rotating around a point is a fairly standard geometric process. Cribbing from this answer:

The way to rotate by an arbitraty point is first substract the point coordinates, do the rotation about the origin and then add the point coordinates.

x2 = px + (x1-px)*cos(q)-(y1-py)*sin(q)
y2 = py + (x1-px)*sin(q)+(y1-py)*cos(q)

where px, py are the rotation point coordinates, and x1,y1 the original 2D shape vertex and x2,y2 the rotated coordinates, and q the angle in radians.

I would imagine that the most efficient way to do this would be to apply the transformation to the 4 vertices and then draw a new rectangle on the rotated canvas.

Community
  • 1
  • 1
sarwar
  • 2,835
  • 1
  • 26
  • 30