I have the following code extracting a sub-image from a JAVA BufferedImage (8-bit grayscale):
// create BufferImage from original image (480px x 400px)
BufferedImage img = new BufferedImage(origImage.getWidth(), origImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
// fill BufferImage with bytes array from original image
img.getRaster().setDataElements(0, 0, img.getWidth(), img.getHeight(), origBytes);
// Crop BufferedImage by range of interest (e.g. 100 x 50 px)
BufferedImage roiSubImage = img.getSubimage(roiLimits[0], roiLimits[2], roiLimits[1], roiLimits[3]);
// convert cropped image back to byte array
DataBufferByte croppedBuffer = (DataBufferByte) roiSubImage.getRaster().getDataBuffer();
bytes[] outputBytes = new byte[croppedBuffer.getData().length];
outputBytes = dataBuffer.getData();
Now – the data in outputBytes are the same as in origImage (as can be expected by the documentation of BufferedImage.getSubImage.).
How can I get the byte array representing only the cropped area of interest, i.e. the 5000 bytes in the area if interest???