robot + 百度 文字识别接口

背景

打算用robot 截取企业微信固定区域,调用百度的文字识别接口,然后识别图片内容,根据内容用robot模拟键盘回复。但是调用百度的接口耗时大概1秒左右,跟我理想的耗时有些出入,放弃后面的继续开发了,把搞好的内容贴到这里吧。

pom依赖

 <properties>
        <baidu.aip.java-sdk.version>4.16.17</baidu.aip.java-sdk.version>
</properties>
<dependencies>
        <!-- https://mvnrepository.com/artifact/com.baidu.aip/java-sdk -->
        <dependency>
            <groupId>com.baidu.aip</groupId>
            <artifactId>java-sdk</artifactId>
            <version>${baidu.aip.java-sdk.version}</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.10.0</version>
        </dependency>
</dependencies>

robot截图


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 截图工具类
 */
public class Img {

    /**
     * 图片存放目录
     */
    public static final String BUFFER_IMAGE_AREA = "C:\\Users\\xxxxx\\scimage\\";

    public static void main(String[] args) throws IOException, AWTException {
        //{左上角的横坐标,左上角的纵坐标,右下角的横坐标,右下角的纵坐标}
        int[] area = {450, 250, 1300, 600};
        String screen = createScreen(area, "test.png");
        System.out.println(screen);
    }

    /**
     * @param area      截图区域,即给定的截图范围:{左上角的横坐标,左上角的纵坐标,右下角的横坐标,右下角的纵坐标}
     * @param imageName 给截取的图片命名
     * @return -
     * @throws AWTException -
     * @throws IOException  -
     */
    public static String createScreen(int[] area, String imageName) throws AWTException, IOException {
        // 电脑屏幕大小
        Dimension screen = null;
        // 截图的宽高
        Rectangle screenRect = null;
        // 暂存图片的缓存
        BufferedImage image = null;
        // 负责截屏的操作者
        Robot robot = null;
        screen = Toolkit.getDefaultToolkit().getScreenSize();
        // 截图尺寸
        screen.height = area[3] - area[1];
        screen.width = area[2] - area[0];
        screenRect = new Rectangle(screen);
        // 左上角得坐标
        screenRect.x = area[0];
        screenRect.y = area[1];
        robot = new Robot();
        // 将得到的屏幕信息存放在缓存里面
        image = robot.createScreenCapture(screenRect);
        // 将缓存里面的屏幕信息以图片的格式存在制定的磁盘位置
        // TODO 这里可以不落到磁盘,后续哪里有需要直接用 
        ImageIO.write(image, getFileSuffix(imageName), new File(BUFFER_IMAGE_AREA, imageName));
        return BUFFER_IMAGE_AREA + imageName;
    }

    public static String getFileSuffix(String path) {
        String[] split = path.split("\\.");
        return split[split.length - 1];
    }
}

百度文字识别接口


import com.baidu.aip.util.Base64Util;
import com.baidu.aip.util.Util;
import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;

/**
 * @Description -
 * @Date 2023-12-20 17:23:38
 * @Version 1.0
 */
public class BaiDuOcr {
    public static final String API_KEY = "QnxxxxDMRCxxxxaxxxxxxD";
    public static final String SECRET_KEY = "rFxxxxxQXO9fxxxxxxy32Hxxxxxe";
    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
    static final String TOKEN = "24xxxxxb49864xxxxxxb6a6bd94f.2592xxxxxx55421.282335-453xxxxx";

    public static void main(String[] args) throws IOException {
        byte[] data = Util.readFileByBytes("C:\\Users\\xxxx\\scimage\\test.png");
        // TODO 这里可以从内存里拿,截图之后直接用,不过磁盘
        String encode = Base64Util.encode(data);
        String base64Content = Util.uriEncode(encode, true);
        StringBuilder sb = new StringBuilder();
        sb.append("image=")
        .append(base64Content)
        .append("&detect_direction=false&detect_language=false&paragraph=false&probability=false");
        System.out.println(sb);
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(sb.toString().trim(), mediaType);
        Request request = new Request.Builder()
                // .url("https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + getAccessToken())
                .url("https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + TOKEN)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                // 申请的文字识别服务的Appid 
                .addHeader("Appid", "45300000")
                .build();
        long begin = System.currentTimeMillis();
        Response response = HTTP_CLIENT.newCall(request).execute();
        long end = System.currentTimeMillis();
        //
        System.out.println("耗时(毫秒):" + (end - begin));
        System.out.println(HTTP_CLIENT);
        assert response.body() != null;
        System.out.println(response.body().string());
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    static String getAccessToken() throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create("grant_type=client_credentials&client_id=" + API_KEY + "&client_secret=" + SECRET_KEY, mediaType);
        Request request = new Request.Builder().url("https://aip.baidubce.com/oauth/2.0/token").method("POST", body).addHeader("Content-Type", "application/x-www-form-urlencoded").build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        assert response.body() != null;
        String accessToken = new JSONObject(response.body().string()).getString("access_token");
        System.out.println(accessToken);
        return accessToken;
    }
}

百度文字识别接口及服务申请

  1. 百度智能云 登录之后,找到控制台
    在这里插入图片描述
  2. 找到 文字识别服务
    在这里插入图片描述
  3. 创建应用
    在这里插入图片描述
  4. 可以在线调试代码 在线调试
    在这里插入图片描述
  5. ok,到这里测试了一下接口的返回,1秒左右。
  6. 拿到文字识别的内容,就可以利用robot玩很多骚操作了(我对时效有要求,这里就到这了,打算换条路子)

鸣谢

  • https://blog.csdn.net/qq_39706570/article/details/120923621
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值