0

I want to capture only the part in the rectangle by clicking the button and save it in storage See Image Here I'm creating an application, the background is camera preview, a rectangle is present at the center(rectangle is created by creating four layouts around the rectangle and setting their background color to partially transparent so that it looks like an overlay is added when I click the capture image button it captures the image of whole screen preview, but I want only the image of part which is present in the rectangle, here is what I have tried,

captuteimageonpro.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               camera.takePicture(null, null, mPictureCallback);
           }
       });


Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
       @Override
       public void onPictureTaken(byte[] data, Camera camera) {

           BitmapFactory.Options options = new BitmapFactory.Options();
           options.inScaled = false;
           Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

           Display display = getWindowManager().getDefaultDisplay();
           Point size = new Point();
           display.getSize(size);
           int width = bitmap.getWidth();
           int height = bitmap.getHeight();

           int start_width = (int) (width * 0.15);
           int start_height = (int) (height * 0.16);
           int end_width = start_width + (int) (width * 0.70);
           int end_height = start_height + (int) (height * 0.52);
           //the decimal values in above lines are the percentages of the rectangle position relative to screen

           int no_pixels = (end_width - start_width) * (end_height - start_height);
           int[] pixels = new int[no_pixels];
           ByteArrayOutputStream bos = new ByteArrayOutputStream();

           bitmap.getPixels(pixels, 0, (end_width - start_width), start_width, start_height, (end_width - start_width), (end_height - start_height));

           bitmap = Bitmap.createBitmap(pixels, 0, (end_width - start_width), (end_width - start_width), (end_height - start_height), Bitmap.Config.ARGB_8888);

           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
           byte[] byteArray = stream.toByteArray();
           bitmap.recycle();

           File picture_file = getOutputMediaFile();
           if(picture_file == null)
           {
               return;
           }
           else {
               try {
                   FileOutputStream fos = new FileOutputStream(picture_file);
                   fos.write(byteArray);

                   fos.close();

                   camera.startPreview();


               }catch (FileNotFoundException e)
               {
                   e.printStackTrace();
               }catch(IOException e)
               {
                   e.printStackTrace();
               }
           }
       }
   };



private File getOutputMediaFile()
   {
       String state = Environment.getExternalStorageState();
       if(!state.equals(Environment.MEDIA_MOUNTED))
       {
           return null;
       }
       else
       {
           File folder_gui = new File(Environment.getExternalStorageDirectory()+ File.separator+"GUI");

           if(!folder_gui.exists())
           {
               folder_gui.mkdirs();
           }
           File outputFile = new File(folder_gui,"temp.jpg");
           return outputFile;
       }

the problem I'm facing with this is that on some phones the clarity of the image captured in that part is so bad, like it is blurred, in some phones the image is saved rotated, can someone help me, are there any changes I should do or is there any other way to do this efficiently.

I don't want to crop the image manually, just after clicking the button the part of the camera preview which is present in the rectangle should be saved in storage.

3
  • Possible duplicate of Crop image in android
    – Sniffer
    Commented Sep 10, 2018 at 7:24
  • @Srijay please let us know if the answer(s) provided where suitable for you Commented Sep 10, 2018 at 13:06
  • @ThomasRichter thanks for the answer, but it didn't solve my problem please check the comment i added to your answer
    – Srijay
    Commented Sep 11, 2018 at 5:21

1 Answer 1

0

I am using a Canvas for this reason. Basically i create an empty canvas with the measurements i need and draw a selected part of the bitmap to the canvas:

private Bitmap cropBitmap(Bitmap bitmap, int x, int y, int width, int height) { 
    Bitmap defaultBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig());
    Canvas canvas = new Canvas(defaultBitmap); 
    canvas.drawBitmap(bitmap, new Rect(x,y,width,height), new Rect(0,0,width,height), new Paint());
    return bitmap;
}

First of all, i am creating an empty background for the canvas, with the measurements i need (width, height and bitmap configuration). Caution: width and height are the measurements of the DESIRED cutted out section of the orginial bitmap. x and y define the top left corner, from where we want to get the section from.

enter image description here

The first one Rect selects the part of the bitmap, that we need to draw to the canvas. The second Rect specifies the location and size of the cropped image on the canvas (full size in this example).

You can also have a look at the canvas documentation here: https://developer.android.com/reference/android/graphics/Canvas#Canvas(android.graphics.Bitmap)

3
  • I have tried what you have said, the whole preview is getting saved in storage, not the part in rectangle, the variables x,y,width,height you mentioned are relative to bitmap.width() and bitmap.height() right?
    – Srijay
    Commented Sep 11, 2018 at 5:19
  • @Srijay: I have updated my answer to clarify how the function should be used. The width and height are NOT the width and height of the bitmap, but the width and height of the section, that needs to be cutted out from the bitmap. x and y specify, the coordinates of the original bitmap, where to start cutting a section out. Commented Sep 12, 2018 at 11:55
  • the problem is solved, the blurred image was due to Picture Size of Camera, I changed the parameters of Camera to set Picture Size. Now the image is clear. Thanks!
    – Srijay
    Commented Sep 20, 2018 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.