Capture image using dynamic co-ordinates through the camera

喜你入骨 提交于 2019-11-29 08:19:27

For me, I take a 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

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);
    //.../
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!