Capture image using dynamic co-ordinates through the camera

前端 未结 2 1330
半阙折子戏
半阙折子戏 2021-01-06 15:27

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 th

相关标签:
2条回答
  • 2021-01-06 16:06

    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

    0 讨论(0)
  • 2021-01-06 16:09

    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);
        //.../
    }
    
    0 讨论(0)
提交回复
热议问题