private Frame padFrameRight(Frame originalFrame, int newWidth) {
Frame.Metadata metadata = originalFrame.getMetadata();
int width = metadata.getWidth();
int height = metadata.getHeight();
Log.i(TAG, "Padded image from: " + width + "x" + height + " to " + newWidth + "x" + height);
ByteBuffer origBuffer = originalFrame.getGrayscaleImageData();
int origOffset = origBuffer.arrayOffset();
byte[] origBytes = origBuffer.array();
ByteBuffer paddedBuffer = ByteBuffer.allocateDirect(newWidth * height);
int paddedOffset = paddedBuffer.arrayOffset();
byte[] paddedBytes = paddedBuffer.array();
Arrays.fill(paddedBytes, (byte) 0);
for (int y = 0; y < height; ++y) {
int origStride = origOffset + y * width;
int paddedStride = paddedOffset + y * newWidth;
System.arraycopy(origBytes, origStride, paddedBytes, paddedStride, width);
}
return new Frame.Builder()
.setImageData(paddedBuffer, newWidth, height, ImageFormat.NV21)
.setId(metadata.getId())
.setRotation(metadata.getRotation())
.setTimestampMillis(metadata.getTimestampMillis())
.build();
}