package com.zzsn.orc;

import org.openqa.selenium.Point;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PicOcr {
    public static void main(String[] args) throws Exception{
        String savePath = "D:/1111.png";
        String savePath1 = "D:/1111222.png";
        BufferedImage  fullImg = ImageIO.read(new File(savePath1));
        BufferedImage eleScreenshot= fullImg.getSubimage(430, 310,
                120, 50);
        eleScreenshot=PicUtil.changeImge(eleScreenshot);

        File outputfile = new File(savePath);
            ImageIO.write(eleScreenshot, "jpg", outputfile);
        String ocrBady = BaiduOCR.imageBasicGeneralOcr(imageToBytes(eleScreenshot));
        List<String> msgList = BaiduOCR.pasermsgJson(ocrBady);

        for (String msg:
                msgList) {
            if(msg.length()>2){
//                code=msg;
            }
            System.out.println(msg);
        }
    }

    private static byte[] imageToBytes(BufferedImage bImage) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(bImage, "png", out);
        } catch (IOException e) {
            //log.error(e.getMessage());
        }
        return out.toByteArray();
    }

    public static List<String> getImageMsg(BufferedImage bufferedImage) throws Exception{
        List<String> msgs=new ArrayList<>();
//        	 logUtil.getLogger().info("---图片切割--");
            BufferedImage splitbufferedImage = PicUtil.img_tailor(bufferedImage, 0,0, 100, 100); // 图片切割
            System.out.println("---图片切割--");
//            splitbufferedImage=PicUtil.changeImge(splitbufferedImage);//图片转黑白
//            String path = "config/image/temp.jpg";//测试的图片位置
//            PicUtil.img2file(splitbufferedImage, "jpg", path);//查看转换的图片
            byte[] splitByte = PicUtil.bufferimage2byte(splitbufferedImage);
            //百度OCR识别信息
            Thread.sleep(3000);
//            String ocrBady = BaiduOCR.imageOcr(splitByte);
//            logUtil.getLogger().info("---BaiduOCR识别--");
            String ocrBady = BaiduOCR.imageBasicGeneralOcr(splitByte);
            List<String> msgList = BaiduOCR.pasermsgJson(ocrBady);
            msgs.addAll(msgList);
        return  msgs;
    }


}
