J'ai une image YUV_420_888 provenant de l'appareil photo. Je veux recadrer un rectangle en niveaux de gris de cette image pour alimenter un algorithme de traitement d'image. Voilà ce que j'ai jusqu'à présent:Découper un rectangle en dehors de Camera2 Image
public static byte[] YUV_420_888toCroppedY(Image image, Rect cropRect) {
byte[] yData;
ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
int ySize = yBuffer.remaining();
yData = new byte[ySize];
yBuffer.get(yData, 0, ySize);
if (cropRect != null) {
int cropArea = (cropRect.right - cropRect.left) * (cropRect.bottom - cropRect.top);
byte[] croppedY = new byte[cropArea];
int cropIndex = 0;
// from the top of the rectangle, to the bottom, sequentially add rows to the output array, croppedY
for (int y = cropRect.top; y < cropRect.top + cropRect.height(); y++) {
// (2x+W) * y + x
int rowStart = (2*cropRect.left + cropRect.width()) * y + cropRect.left;
// (2x+W) * y + x + W
int rowEnd = (2*cropRect.left + cropRect.width()) * y + cropRect.left + cropRect.width();
for (int x = rowStart; x < rowEnd; x++) {
croppedY[cropIndex] = yData[x];
cropIndex++;
}
}
return croppedY;
}
return yData;
}
Cette fonction fonctionne sans erreur, mais l'image que je sors de c'est des ordures - il ressemble à ceci:
Je ne suis pas sûr comment résoudre ce problème ou ce que je fais mal.
Je pense que vous devriez afficher l'image originale –
@YandryPozo Je ne l'ai pas, je regardais à travers mon écran d'aperçu et il ne ressemble pas à ce – Carpetfizz
Vous pourriez trouver la réponse en lisant sur les tampons gralloc et android: https://source.android.com/devices/graphics/arch-bq-gralloc. Ceci est un bon aperçu qui explique aussi pourquoi la largeur du tampon en octets peut différer de la largeur de l'image en pixels: https://www.codeproject.com/articles/991640/androids-graphics-buffer-management-system -part-je – l85m