Capture image using dynamic co-ordinates through t

2019-01-27 03:50发布

问题:

I am making a camera based application, where I put a rectangular view over the camera. When I capture an image using new Camera.PictureCallback(), I cropped that image so as it will get the part of the rectangle. Well, its working fine.

Now I implemented View.OnTouchListener and using that I made the shape movable.

So, I need to capture the image with the final selection of the user, like where they place the rectangle.

Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length); // 2560×1440
                float scale = 1280 / 1000;
                int left = (int) (scale * (imageOriginal.getWidth() - 250) / 2);
                int top = (int) (scale * (imageOriginal.getHeight() - 616) / 2);
                int width = (int) (scale * 750);
                int height = (int) (scale * 616);
                Bitmap imageConverted = Bitmap.createBitmap(imageOriginal, left, top, width, height, null, false);

This is the method i used to crop image.The values are hard corded to find the exact position. Now i need values for that top ,bottom, height, width with the changing rectangle.

//My customView that used to draw that rectangle

public class CustomView extends View  {
    private Paint paint = new Paint();
    public CustomView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) { // Override the onDraw() Method
        super.onDraw(canvas);

        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(10);

        //center
        int x0 = canvas.getWidth()/2;
        int y0 = canvas.getHeight()/2;
        int dx = canvas.getHeight()/3;
        int dy = canvas.getHeight()/3;
        //draw guide box
        canvas.drawRect(x0-dx, y0-dy, x0+dx, y0+dy, paint);
    }


}

//my picture callback code

  Camera.PictureCallback mPicture = new Camera.PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {

            // Replacing the button after a photho was taken.


            // File name of the image that we just took.
            fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()).toString() + ".jpg";

            // Creating the directory where to save the image. Sadly in older
            // version of Android we can not get the Media catalog name
            File mkDir = new File(sdRoot, dir);
            mkDir.mkdirs();

            // Main file where to save the data that we recive from the camera
            File pictureFile = new File(sdRoot, dir + fileName);

            // Cropping image with the corresponding co-ordinates and save in to a file
            try {

                Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length); // 2560×1440
                float scale = 1280 / 1000;
                int left = (int) (scale * (imageOriginal.getWidth() - 250) / 2);
                int top = (int) (scale * (imageOriginal.getHeight() - 616) / 2);
                int width = (int) (scale * 750);
                int height = (int) (scale * 616);
                Bitmap imageConverted = Bitmap.createBitmap(imageOriginal, left, top, width, height, null, false);

                FileOutputStream purge = new FileOutputStream(pictureFile);
                imageConverted.compress(Bitmap.CompressFormat.JPEG, 100, purge);
                purge.flush();
                purge.close();

            } catch (FileNotFoundException e) {
                Log.d("DG_DEBUG", "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d("DG_DEBUG", "Error accessing file: " + e.getMessage());
            }

            // Adding Exif data for the orientation.
            try {
                ProjectManager.getInstance().settings.IMAGE_LOCATION = "/sdcard/" + dir + fileName;
                exif = new ExifInterface(ProjectManager.getInstance().settings.IMAGE_LOCATION);
                exif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + orientation);
                exif.saveAttributes();
                mView.saveImage(dir + fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    };

回答1:

For me, I take whole image then crop according to dimension of the selected area of the image..

public void onPictureTaken(byte[] data, Camera camera) {


                Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length, null);
                int width = imageOriginal.getWidth();
                int height = imageOriginal.getHeight(); // for width
                int narrowSize = Math.min(width, height); // for height
                int differ = (int) Math.abs((imageOriginal.getHeight() - imageOriginal.getWidth()) / 2.0f);  // for dimension
                width = (width == narrowSize) ? 0 : differ;
                height = (width == 0) ? differ : 0; 

                Matrix rotationMatrix = new Matrix();
                rotationMatrix.postRotate(90); // for orientation

                Bitmap imageCropped = Bitmap.createBitmap(imageOriginal, width, height, narrowSize, narrowSize, rotationMatrix, false);


    }

Capture Image

Output Image



回答2:

Your rectangle is mCustomView.getWidth()*2/3 pixels wide, and mCustomView.getHeight()*2/3 pixels high, and its left corner is at 1/6 of mCustomView.getWidth(), if I understand your post correctly.

public void onPictureTaken(byte[] data, Camera camera) {
    //.../
    Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length);
    int[] customViewPosition;
    mCustomView.getLocationInWindow(customViewPosition);
    int[] surfacePosition;
    mSurfaceView.getLocationInWindow(surfacePosition);

    float scale = imageOriginal.getWidth()/(float)mSurfaceView.getWidth();
    int left = (int) scale*(customViewPosition[0] + mCustomView.getWidth()/6F - surfacePosition[0]);
    int top = (int) scale*(customViewPosition[1] + mCustomView.getHeight()/6F - surfacePosition[1]);
    int width = (int) scale*mCustomView.getWidth()*2/3;
    int height = (int) scale*mCustomView.getHeight()*2/3;
    Bitmap imageCropped = Bitmap.createBitmap(imageOriginal, left, top, width, height, null, false);
    //.../
}