package com.example.administrator.zhapp.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.annotation.IntegerRes;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

/**
 * Created by gzw on 2016/11/7.
 */

public class BitmapString {

    /**
     * 图片转成16进制string类型，返回打印图片的语句
     *
     * @param bitmap
     * @return
     */
    public static String convertIconToString(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] appicon = baos.toByteArray();// 转为byte数组
        //数组转换成十六进制字符串
        StringBuffer sb = new StringBuffer(appicon.length);
        String sTemp;
        for (int i = 0; i < appicon.length; i++) {
            sTemp = Integer.toHexString(0xFF & appicon[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }

//        StringBuffer sb = new StringBuffer();
//        long clr = 0;
//        long n = 0;
//        int b = 0;
//        for (int i = 0; i < bitmap.getHeight(); i++) {
//            for (int j = 0; j < bitmap.getWidth(); j++) {
//                b = b * 2;
////                clr = bitmap.GetPixel(j, i).ToArgb();
//                clr = bitmap.getPixel(j, i);
//                String s = Long.toHexString(clr);
//
//                if (s.substring(s.length() - 6, 6).compareTo("BBBBBB") < 0) {
//                    b++;
//                }
//                n++;
//                if (j == (bitmap.getWidth() - 1)) {
//                    if (n < 8) {
//                        b = b * (2 ^ (8 - (int) n));
//                        String str = Integer.toHexString(b);
//                        if (str.length() == 2) {
//
//                        } else if (str.length() == 1) {
//                            str = "0" + str;
//                        } else if (str.length() == 0) {
//                            str = "00" + str;
//                        }
//                        sb.append(str);
//                        b = 0;
//                        n = 0;
//                    }
//                }
//                if (n >= 8) {
//
//                    String str = Integer.toHexString(b);
//                    if (str.length() == 2) {
//
//                    } else if (str.length() == 1) {
//                        str = "0" + str;
//                    } else if (str.length() == 0) {
//                        str = "00" + str;
//                    }
//
//                    sb.append(str);
//                    b = 0;
//                    n = 0;
//                }
//            }
//            sb.append("/n");
//        }

        int t = ((bitmap.getWidth() / 8 + ((bitmap.getWidth() % 8 == 0) ? 0 : 1)) * bitmap.getHeight());
        int w = (bitmap.getWidth() / 8 + ((bitmap.getWidth() % 8 == 0) ? 0 : 1));
        String str = "~DGR:" + "AA" + ".GRF," + t + "," + w + "," + sb;
        return str;
    }

    /**
     * string转成bitmap  返回打印的语句
     *
     * @param st
     */
    public static String convertStringToIcon(String st, String name) {
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(st, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
                    bitmapArray.length);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] appicon = baos.toByteArray();// 转为byte数组
            //数组转换成十六进制字符串
            StringBuffer sb = new StringBuffer(appicon.length);
            String sTemp;
            for (int i = 0; i < appicon.length; i++) {
                sTemp = Integer.toHexString(0xFF & appicon[i]);
                if (sTemp.length() < 2)
                    sb.append(0);
                sb.append(sTemp.toUpperCase());
            }
            int t = ((bitmap.getWidth() / 8 + ((bitmap.getWidth() % 8 == 0) ? 0 : 1)) * bitmap.getHeight());
            int w = (bitmap.getWidth() / 8 + ((bitmap.getWidth() % 8 == 0) ? 0 : 1));
            String str = "~DGd:" + name + ".GRF," + t + "," + w + "," + sb;
            return str;
        } catch (Exception e) {
            System.out.println("==============异常=================");
            e.printStackTrace();
            return null;
        }
    }
}