package com.sagereal.print.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import java.lang.ref.WeakReference;
import java.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class QrcodeOrBarcode {
    private static int height;
    private static int width;

    public static byte[] getPrintPhotoByteArray(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        try {
            int i = (height * 384) / width;
            Bitmap bitmapZoomImg = BitmapUtils.zoomImg(bitmap, 384, i);
            int width2 = bitmapZoomImg.getWidth();
            int height2 = bitmapZoomImg.getHeight();
            int i2 = width2 * 3;
            int i3 = ((width2 % 4) + i2) * height2;
            int i4 = i3 + 54;
            byte[] bArr = new byte[i4];
            int i5 = 0;
            System.arraycopy(DataFormatConversion.writeWord(19778), 0, bArr, 0, 2);
            System.arraycopy(DataFormatConversion.writeDword(i4), 0, bArr, 2, 4);
            long j = 0;
            System.arraycopy(DataFormatConversion.writeDword(j), 0, bArr, 6, 2);
            System.arraycopy(DataFormatConversion.writeDword(j), 0, bArr, 8, 2);
            System.arraycopy(DataFormatConversion.writeDword(54L), 0, bArr, 10, 4);
            Log.d("jiangcunbin", "totalSize14     :  " + Arrays.toString(bArr));
            Bitmap bitmap2 = bitmapZoomImg;
            System.arraycopy(DataFormatConversion.writeDword(40L), 0, bArr, 14, 4);
            System.arraycopy(DataFormatConversion.writeLong((long) width2), 0, bArr, 18, 4);
            System.arraycopy(DataFormatConversion.writeLong(height2), 0, bArr, 22, 4);
            System.arraycopy(DataFormatConversion.writeWord(1), 0, bArr, 26, 2);
            System.arraycopy(DataFormatConversion.writeWord(24), 0, bArr, 28, 2);
            System.arraycopy(DataFormatConversion.writeDword(0L), 0, bArr, 30, 4);
            System.arraycopy(DataFormatConversion.writeDword(((long) i) * 1152), 0, bArr, 34, 4);
            System.arraycopy(DataFormatConversion.writeLong(0L), 0, bArr, 38, 4);
            System.arraycopy(DataFormatConversion.writeLong(0L), 0, bArr, 42, 4);
            System.arraycopy(DataFormatConversion.writeDword(0L), 0, bArr, 46, 4);
            System.arraycopy(DataFormatConversion.writeDword(0L), 0, bArr, 50, 4);
            Log.d("jiangcunbin", "totalSize54     :  " + Arrays.toString(bArr));
            byte[] bArr2 = new byte[i3];
            int i6 = i2 + (width2 % 4);
            int i7 = height2 + (-1);
            int i8 = 0;
            while (i8 < height2) {
                int i9 = i5;
                int i10 = i9;
                while (i9 < width2) {
                    Bitmap bitmap3 = bitmap2;
                    int pixel = bitmap3.getPixel(i9, i8);
                    int i11 = (i7 * i6) + i10;
                    bArr2[i11] = (byte) Color.blue(pixel);
                    bArr2[i11 + 1] = (byte) Color.green(pixel);
                    bArr2[i11 + 2] = (byte) Color.red(pixel);
                    i9++;
                    i10 += 3;
                    bitmap2 = bitmap3;
                }
                i8++;
                i7--;
                i5 = 0;
            }
            System.arraycopy(bArr2, 0, bArr, 54, i3);
            return bArr;
        } catch (Exception e) {
            Log.d("jiangcunbin", "  Exception   print QRcode" + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    public static Bitmap convertToBitmap(String str) {
        float f;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        BitmapFactory.decodeFile(str, options);
        width = options.outWidth;
        height = options.outHeight;
        int i = (height * 384) / width;
        Log.e("jiangcunbin", "photo height is :  " + height);
        float f2 = 0.0f;
        if (width > 384 || height > i) {
            f2 = width / 384;
            f = height / i;
        } else {
            f = 0.0f;
        }
        options.inJustDecodeBounds = false;
        options.inSampleSize = (int) Math.max(f2, f);
        return Bitmap.createScaledBitmap((Bitmap) new WeakReference(BitmapFactory.decodeFile(str, options)).get(), 384, i, true);
    }
}