How to crop a bitmap as of same size of a rectangular imageview?

拟墨画扇 提交于 2020-06-09 07:10:03

问题


I have a SurfaceView on screen and i want to know its starting x,y position on screen and also its width and height so as i can crop the full screen image only to the SurfaceView size part means whatever fits in that bounds and the rest of the part to be chopped off. The code I am trying is:

Bitmap bitmap = BitmapFactory.decodeByteArray(mCameraData, 0, mCameraData.length);

int[] location = new int[2];
mCameraPreview.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
Bitmap resizedbitmap1=Bitmap.createBitmap(bitmap, x,y,mCameraPreview.getWidth(), mCameraPreview.getHeight());
mCameraImage.setImageBitmap(resizedbitmap1);

But it is taking some left part of the screen from the image. Please help.

Edit:

private void setupImageDisplay() {
            Bitmap bitmap = BitmapFactory.decodeByteArray(mCameraData, 0, mCameraData.length);

            int[] location = new int[2];
            mCameraPreview.getLocationOnScreen(location);  //view
            int x = location[0];
            int y = location[1];
            Bitmap resizedbitmap1=Bitmap.createBitmap(bitmap, x,y,mCameraPreview.getWidth(), mCameraPreview.getHeight());

            mCameraImage.setImageBitmap(resizedbitmap1);
            mCamera.stopPreview();
            mCameraPreview.setVisibility(View.INVISIBLE);
            s1.setVisibility(View.INVISIBLE);
            v1.setVisibility(View.INVISIBLE);
            mCameraImage.setVisibility(View.VISIBLE);
            mCaptureImageButton.setText(R.string.recapture_image);
            mCaptureImageButton.setOnClickListener(mRecaptureImageButtonClickListener);
        }

But my SurfaceView doesn't cover the full screen.

New Edit:

Here's the screen to demonstrate what i want:

I want the part of the image only in blue rectangle. So please help.

来源:https://stackoverflow.com/questions/61732571/how-to-crop-a-bitmap-as-of-same-size-of-a-rectangular-imageview

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