|
3 | 3 | import android.graphics.Bitmap; |
4 | 4 | import android.graphics.BitmapFactory; |
5 | 5 | import android.graphics.ImageFormat; |
| 6 | +import android.graphics.Matrix; |
6 | 7 | import android.graphics.Rect; |
7 | 8 | import android.graphics.YuvImage; |
8 | 9 | import android.os.Environment; |
@@ -78,16 +79,42 @@ public SparseArray<Face> detect(Frame frame) { |
78 | 79 | if (!IsBusy && y > 0 && recognitionHandler != null) { |
79 | 80 | IsBusy = true; |
80 | 81 |
|
| 82 | + int fHeight = frame.getMetadata().getHeight(); |
| 83 | + int fWidth = frame.getMetadata().getWidth(); |
81 | 84 | YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21, |
82 | | - frame.getMetadata().getWidth(), frame.getMetadata().getHeight(), null); |
| 85 | + fWidth, fHeight, null); |
83 | 86 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
84 | | - yuvImage.compressToJpeg(new Rect(0, 0, frame.getMetadata().getWidth(), |
85 | | - frame.getMetadata().getHeight()), 100, byteArrayOutputStream); |
| 87 | + yuvImage.compressToJpeg(new Rect(0, 0, fWidth, fHeight), |
| 88 | + 100, byteArrayOutputStream); |
86 | 89 | byte[] jpegArray = byteArrayOutputStream.toByteArray(); |
87 | | - int orientation = Exif.getOrientation(jpegArray); |
88 | | - Log.w(TAG, String.format("Orientation %d", orientation)); |
89 | 90 | Bitmap tmpBitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length); |
90 | | - final Bitmap cropped = Bitmap.createBitmap(tmpBitmap, x, y, w, h); |
| 91 | + final Bitmap cropped; |
| 92 | + Matrix rot = new Matrix(); |
| 93 | + switch (frame.getMetadata().getRotation()) |
| 94 | + { |
| 95 | + case 1: |
| 96 | + Log.i(TAG, "orientation 1"); |
| 97 | + Log.i(TAG, String.format("frame height %d", fHeight)); |
| 98 | + rot.postRotate(90); |
| 99 | + cropped = Bitmap.createBitmap(tmpBitmap, y, fHeight - (x + w), h, w, |
| 100 | + rot,false ); |
| 101 | + break; |
| 102 | + case 2: |
| 103 | + Log.i(TAG, "orientation 2"); |
| 104 | + rot.postRotate(180); |
| 105 | + cropped = Bitmap.createBitmap(tmpBitmap, fWidth - (x + w), |
| 106 | + fHeight - (y + h), w, h, rot, false); |
| 107 | + break; |
| 108 | + case 3: |
| 109 | + Log.i(TAG, "orientation 3"); |
| 110 | + rot.postRotate(270); |
| 111 | + cropped = Bitmap.createBitmap(tmpBitmap, fWidth - (y + h), x, h, w, rot, false); |
| 112 | + break; |
| 113 | + default: |
| 114 | + Log.i(TAG, "orientation 0"); |
| 115 | + cropped = Bitmap.createBitmap(tmpBitmap, x, y, w, h); |
| 116 | + break; |
| 117 | + } |
91 | 118 | // try { |
92 | 119 | // |
93 | 120 | // File file = new File (getExternalStorageDirectory(), "/Download/test1.bmp"); |
|
0 commit comments