177 lines
7.0 KiB
Java
177 lines
7.0 KiB
Java
|
package com.google.firebase.ml.vision.common;
|
||
|
|
||
|
import android.graphics.Bitmap;
|
||
|
import android.graphics.BitmapFactory;
|
||
|
import android.graphics.Matrix;
|
||
|
import android.os.SystemClock;
|
||
|
import android.util.Pair;
|
||
|
import com.google.android.gms.common.internal.Preconditions;
|
||
|
import com.google.android.gms.internal.firebase_ml.zzpv;
|
||
|
import com.google.android.gms.internal.firebase_ml.zzpx;
|
||
|
import com.google.android.gms.vision.Frame;
|
||
|
import com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata;
|
||
|
import java.nio.ByteBuffer;
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
public class FirebaseVisionImage {
|
||
|
private static zzpx zzaxa = zzpx.zznk();
|
||
|
private volatile Bitmap zzaxb;
|
||
|
private volatile ByteBuffer zzaxc;
|
||
|
private volatile FirebaseVisionImageMetadata zzaxd;
|
||
|
private volatile Frame zzaxe;
|
||
|
private volatile byte[] zzaxf;
|
||
|
private final long zzaxg = SystemClock.elapsedRealtime();
|
||
|
|
||
|
public static FirebaseVisionImage fromByteBuffer(ByteBuffer byteBuffer, FirebaseVisionImageMetadata firebaseVisionImageMetadata) {
|
||
|
return new FirebaseVisionImage(byteBuffer, firebaseVisionImageMetadata);
|
||
|
}
|
||
|
|
||
|
private FirebaseVisionImage(ByteBuffer byteBuffer, FirebaseVisionImageMetadata firebaseVisionImageMetadata) {
|
||
|
this.zzaxc = (ByteBuffer) Preconditions.checkNotNull(byteBuffer);
|
||
|
this.zzaxd = (FirebaseVisionImageMetadata) Preconditions.checkNotNull(firebaseVisionImageMetadata);
|
||
|
}
|
||
|
|
||
|
public Bitmap getBitmapForDebugging() {
|
||
|
return zznj();
|
||
|
}
|
||
|
|
||
|
private final Bitmap zznj() {
|
||
|
if (this.zzaxb != null) {
|
||
|
return this.zzaxb;
|
||
|
}
|
||
|
synchronized (this) {
|
||
|
if (this.zzaxb == null) {
|
||
|
byte[] zzae = zzae(false);
|
||
|
Bitmap decodeByteArray = BitmapFactory.decodeByteArray(zzae, 0, zzae.length);
|
||
|
if (this.zzaxd != null) {
|
||
|
decodeByteArray = zza(decodeByteArray, this.zzaxd.getRotation());
|
||
|
}
|
||
|
this.zzaxb = decodeByteArray;
|
||
|
}
|
||
|
}
|
||
|
return this.zzaxb;
|
||
|
}
|
||
|
|
||
|
private static Bitmap zza(Bitmap bitmap, int i) {
|
||
|
int zzbm = zzpv.zzbm(i);
|
||
|
if (zzbm == 0) {
|
||
|
return bitmap;
|
||
|
}
|
||
|
Matrix matrix = new Matrix();
|
||
|
matrix.postRotate(zzbm);
|
||
|
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
|
||
|
}
|
||
|
|
||
|
public final Pair<byte[], Float> zze(int i, int i2) {
|
||
|
int width;
|
||
|
int height;
|
||
|
byte[] zzae;
|
||
|
if (this.zzaxd != null) {
|
||
|
boolean z = this.zzaxd.getRotation() == 1 || this.zzaxd.getRotation() == 3;
|
||
|
FirebaseVisionImageMetadata firebaseVisionImageMetadata = this.zzaxd;
|
||
|
width = z ? firebaseVisionImageMetadata.getHeight() : firebaseVisionImageMetadata.getWidth();
|
||
|
height = z ? this.zzaxd.getWidth() : this.zzaxd.getHeight();
|
||
|
} else {
|
||
|
width = zznj().getWidth();
|
||
|
height = zznj().getHeight();
|
||
|
}
|
||
|
float min = Math.min(i / width, i2 / height);
|
||
|
if (min < 1.0f) {
|
||
|
Bitmap zznj = zznj();
|
||
|
Matrix matrix = new Matrix();
|
||
|
matrix.postScale(min, min);
|
||
|
zzae = zzpx.zza(Bitmap.createBitmap(zznj, 0, 0, this.zzaxb.getWidth(), this.zzaxb.getHeight(), matrix, true));
|
||
|
} else {
|
||
|
zzae = zzae(true);
|
||
|
min = 1.0f;
|
||
|
}
|
||
|
return Pair.create(zzae, Float.valueOf(min));
|
||
|
}
|
||
|
|
||
|
private final byte[] zzae(boolean z) {
|
||
|
if (this.zzaxf != null) {
|
||
|
return this.zzaxf;
|
||
|
}
|
||
|
synchronized (this) {
|
||
|
if (this.zzaxf != null) {
|
||
|
return this.zzaxf;
|
||
|
}
|
||
|
if (this.zzaxc != null && (!z || this.zzaxd.getRotation() == 0)) {
|
||
|
byte[] zza = zzpx.zza(this.zzaxc);
|
||
|
int format = this.zzaxd.getFormat();
|
||
|
if (format != 17) {
|
||
|
if (format == 842094169) {
|
||
|
zza = zzpx.zzf(zza);
|
||
|
} else {
|
||
|
throw new IllegalStateException("Must be one of: IMAGE_FORMAT_NV21, IMAGE_FORMAT_YV12");
|
||
|
}
|
||
|
}
|
||
|
byte[] zza2 = zzpx.zza(zza, this.zzaxd.getWidth(), this.zzaxd.getHeight());
|
||
|
if (this.zzaxd.getRotation() == 0) {
|
||
|
this.zzaxf = zza2;
|
||
|
}
|
||
|
return zza2;
|
||
|
}
|
||
|
byte[] zza3 = zzpx.zza(zznj());
|
||
|
this.zzaxf = zza3;
|
||
|
return zza3;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public final Frame zza(boolean z, boolean z2) {
|
||
|
Frame frame;
|
||
|
synchronized (this) {
|
||
|
int i = 0;
|
||
|
Preconditions.checkArgument((z && z2) ? false : true, "Can't restrict to bitmap-only and NV21 byte buffer-only");
|
||
|
if (this.zzaxe == null) {
|
||
|
Frame.Builder builder = new Frame.Builder();
|
||
|
if (this.zzaxc != null && !z) {
|
||
|
int i2 = 842094169;
|
||
|
if (z2 && this.zzaxd.getFormat() != 17) {
|
||
|
if (this.zzaxd.getFormat() == 842094169) {
|
||
|
this.zzaxc = ByteBuffer.wrap(zzpx.zzf(zzpx.zza(this.zzaxc)));
|
||
|
this.zzaxd = new FirebaseVisionImageMetadata.Builder().setFormat(17).setWidth(this.zzaxd.getWidth()).setHeight(this.zzaxd.getHeight()).setRotation(this.zzaxd.getRotation()).build();
|
||
|
} else {
|
||
|
throw new IllegalStateException("Must be one of: IMAGE_FORMAT_NV21, IMAGE_FORMAT_YV12");
|
||
|
}
|
||
|
}
|
||
|
ByteBuffer byteBuffer = this.zzaxc;
|
||
|
int width = this.zzaxd.getWidth();
|
||
|
int height = this.zzaxd.getHeight();
|
||
|
int format = this.zzaxd.getFormat();
|
||
|
if (format == 17) {
|
||
|
i2 = 17;
|
||
|
} else if (format != 842094169) {
|
||
|
i2 = 0;
|
||
|
}
|
||
|
builder.setImageData(byteBuffer, width, height, i2);
|
||
|
int rotation = this.zzaxd.getRotation();
|
||
|
if (rotation != 0) {
|
||
|
if (rotation != 1) {
|
||
|
i = 2;
|
||
|
if (rotation != 2) {
|
||
|
i = 3;
|
||
|
if (rotation != 3) {
|
||
|
StringBuilder sb = new StringBuilder(29);
|
||
|
sb.append("Invalid rotation: ");
|
||
|
sb.append(rotation);
|
||
|
throw new IllegalArgumentException(sb.toString());
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
i = 1;
|
||
|
}
|
||
|
}
|
||
|
builder.setRotation(i);
|
||
|
} else {
|
||
|
builder.setBitmap(zznj());
|
||
|
}
|
||
|
builder.setTimestampMillis(this.zzaxg);
|
||
|
this.zzaxe = builder.build();
|
||
|
}
|
||
|
frame = this.zzaxe;
|
||
|
}
|
||
|
return frame;
|
||
|
}
|
||
|
}
|