3

Am creating a document scanning application in android, am using OpenCV and Scan library in my project for cropping,I have created a rectangle using drawrect in camera view, now I need to capture the images inside that rectangle portion only and display it in another activity.

The image in question:

enter image description here

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
balajivaishnav
  • 2,795
  • 4
  • 23
  • 38

2 Answers2

5

For me , I will take whole image, then crop. Your question : "how do I know which part of the image is inside the rectangular portion, then only I can pass it nah, hope u understood". My answer is you can using relativity scaling of whole image dimension and camera display screen dimension. Then you will know which part of rectangular to be cropped.

This is the code example. Note that you need to fill some codes to make it can save file into jpg, and save it after cropped.

    // 1. Save your bitmap to file
    public class MyPictureCallback implements Camera.PictureCallback  {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        try {
            //mPictureFile is a file to save the captured image
            FileOutputStream fos = new FileOutputStream(mPictureFile);
            fos.write(data);
            fos.close();

        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        }
    }
    }

    // Somewhere in your code
    // 2.1 Load bitmap from your .jpg file              
    Bitmap bitmap = BitmapFactory.decodeFile(path+"/mPictureFile_name.jpg");

    // 2.2 Rotate the bitmap to be the same as display, if need.
    ... Add some bitmap rotate code

    // 2.3 Size of rotated bitmap
    int bitWidth = bitmap.getWidth();
    int bitHeight = bitmap.getHeight();

    // 3. Size of camera preview on screen  
    int preWidth = preview.getWidth();
    int preHeight = preview.getHeight();

    // 4. Scale it. 
    // Assume you draw Rect as "canvas.drawRect(60, 50, 210, 297, paint);" command
    int startx = 60 * bitWidth / preWidth;
    int starty = 50 * bitHeight / preHeight;
    int endx = 210 * bitWidth / preWidth;
    int endy = 297 * bitHeight / preHeight;

    // 5. Crop image
    Bitmap blueArea = Bitmap.createBitmap(bitmap, startx, starty, endx, endy);

    // 6. Save Crop bitmap to file
-1

This will work for you: How to programmatically take a screenshot in Android?

Make sure that the view (v1 in the code sample's case) passed in Bitmap.createBitmap(v1.getDrawingCache()) is a viewgroup that contains the image you want ot send to the second activity

Edit: I don't think your intended flow is feasible. As far as I know, camera intents don't take arguments allowing to draw such a rectangle (I could be wrong though).

Instead, I suggest you take a picture, and then edit it with a library such as this one (https://github.com/ArthurHub/Android-Image-Cropper) or programatically as suggested above.

Community
  • 1
  • 1
Bruno Carrier
  • 530
  • 5
  • 8