Skip to content

Commit 74faab8

Browse files
author
Vladimir Kocheryzhkin
committed
rotate cropped bitmap
1 parent a962582 commit 74faab8

File tree

1 file changed

+33
-6
lines changed
  • visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker

1 file changed

+33
-6
lines changed

visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/CustomDetector.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
55
import android.graphics.ImageFormat;
6+
import android.graphics.Matrix;
67
import android.graphics.Rect;
78
import android.graphics.YuvImage;
89
import android.os.Environment;
@@ -78,16 +79,42 @@ public SparseArray<Face> detect(Frame frame) {
7879
if (!IsBusy && y > 0 && recognitionHandler != null) {
7980
IsBusy = true;
8081

82+
int fHeight = frame.getMetadata().getHeight();
83+
int fWidth = frame.getMetadata().getWidth();
8184
YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21,
82-
frame.getMetadata().getWidth(), frame.getMetadata().getHeight(), null);
85+
fWidth, fHeight, null);
8386
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);
8689
byte[] jpegArray = byteArrayOutputStream.toByteArray();
87-
int orientation = Exif.getOrientation(jpegArray);
88-
Log.w(TAG, String.format("Orientation %d", orientation));
8990
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+
}
91118
// try {
92119
//
93120
// File file = new File (getExternalStorageDirectory(), "/Download/test1.bmp");

0 commit comments

Comments
 (0)