图像识别 Image-认证鉴权:示例代码
时间:2025-02-12 14:58:00
示例代码
下面代码展示了如何对一个请求进行签名,并通过AisAccess发送一个HTTPS请求的过程:
代码分成两个类进行演示:
ResponseProcessUtils:工具类,用于处理结果的返回。
ImageTaggingDemo:进行相关参数ak,sk,region的配置,访问 图像标签服务 的示例。
- ResponseProcessUtils.java
1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
package com.huawei.ais.demo; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import org.apache.http.HttpResponse; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.cloud.sdk.util.Base64; import com.huawei.ais.sdk.util.HttpClientUtils; /** * 访问服务返回结果信息验证的工具类 */ public class ResponseProcessUtils { /** * 打印出服务访问完成的HTTP状态码 * * @param response 响应对象 */ public static void processResponseStatus(HttpResponse response) { System.out.println(response.getStatusLine().getStatusCode()); } /** * 打印出服务访问完成后,转化为文本的字符流,主要用于JSON数据的展示 * * @param response 响应对象 * @throws UnsupportedOperationException * @throws IOException */ public static void processResponse(HttpResponse response) throws UnsupportedOperationException, IOException { System.out.println(HttpClientUtils.convertStreamToString(response.getEntity().getContent())); } /** * 处理返回Base64编码的图像文件的生成 * * @param response * @throws UnsupportedOperationException * @throws IOException */ public static void processResponseWithImage(HttpResponse response, String fileName) throws UnsupportedOperationException, IOException { String result = HttpClientUtils.convertStreamToString(response.getEntity().getContent()); JSONObject resp = JSON.parseObject(result); String imageString = (String)resp.get("result"); byte[] fileBytes = Base64.decode(imageString); writeBytesToFile(fileName, fileBytes); } /** * 将字节数组写入到文件, 用于支持二进制文件(如图片)的生成 * @param fileName 文件名 * @param data 数据 * @throws IOException */ public static void writeBytesToFile(String fileName, byte[] data) throws IOException{ FileChannel fc = null; try { ByteBuffer bb = ByteBuffer.wrap(data); fc = new FileOutputStream(fileName).getChannel(); fc.write(bb); } catch (Exception e) { e.printStackTrace(); System.out.println(e.getMessage()); } finally { fc.close(); } } }
- ImageTaggingDemo.java
1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
package com.huawei.ais.demo.image;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.alibaba.fastjson.serializer.SerializerFeature;import com.huawei.ais.demo.ResponseProcessUtils;import com.huawei.ais.demo.ServiceAccessBuilder;import com.huawei.ais.sdk.AisAccess;import com.huawei.ais.sdk.util.HttpClientUtils;import org.apache.commons.codec.binary.Base64;import org.apache.commons.io.FileUtils;import org.apache.http.HttpResponse;import org.apache.http.entity.StringEntity;import java.io.File;import java.io.IOException;/** * 图像标签服务的使用示例类 */public class ImageTaggingDemo {//// 图像标签服务的使用示例函数//private static void imageTaggingDemo() throws IOException {// 1. 图像标签服务的的基本信息,生成对应的一个客户端连接对象AisAccess service = ServiceAccessBuilder.builder() .ak("######") // your ak .sk("######") // your sk .region("cn-north-1") // 图像识别服务华北-北京一的配置 .connectionTimeout(5000) // 连接目标url超时限制 .connectionRequestTimeout(1000) // 连接池获取可用连接超时限制 .socketTimeout(20000) // 获取服务器响应数据超时限制 .build();try {//// 2.构建访问图像标签服务需要的参数//String uri = "/v1.0/image/tagging";byte[] fileData = FileUtils.readFileToByteArray(new File("data/image-tagging-demo-1.jpg"));String fileBase64Str = Base64.encodeBase64String(fileData);JSONObject json = new JSONObject();json.put("image", fileBase64Str);json.put("threshold", 60);StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8");// 3.传入图像标签服务对应的uri参数, 传入图像标签服务需要的参数,// 该参数主要通过JSON对象的方式传入, 使用POST方法调用服务HttpResponse response = service.post(uri, stringEntity);// 4.验证服务调用返回的状态是否成功,如果为200, 为成功, 否则失败。ResponseProcessUtils.processResponseStatus(response);// 5.处理服务返回的字符流,输出识别结果。JSONObject jsonObject = JSON.parseObject(HttpClientUtils.convertStreamToString(response.getEntity().getContent()));System.out.println(JSON.toJSONString(JSON.parse(jsonObject.toString()), SerializerFeature.PrettyFormat));} catch (Exception e) {e.printStackTrace();} finally {// 6.使用完毕,关闭服务的客户端连接service.close();}}//// 主入口函数//public static void main(String[] args) throws IOException {// 测试入口函数imageTaggingDemo();}}
support.huaweicloud.com/api-image/image_03_0003.html
看了此文的人还看了
CDN加速
GaussDB
文字转换成语音
免费的服务器
如何创建网站
域名网站购买
私有云桌面
云主机哪个好
域名怎么备案
手机云电脑
SSL证书申请
云点播服务器
免费OCR是什么
电脑云桌面
域名备案怎么弄
语音转文字
文字图片识别
云桌面是什么
网址安全检测
网站建设搭建
国外CDN加速
SSL免费证书申请
短信批量发送
图片OCR识别
云数据库MySQL
个人域名购买
录音转文字
扫描图片识别文字
OCR图片识别
行驶证识别
虚拟电话号码
电话呼叫中心软件
怎么制作一个网站
Email注册网站
华为VNC
图像文字识别
企业网站制作
个人网站搭建
华为云计算
免费租用云托管
云桌面云服务器
ocr文字识别免费版
HTTPS证书申请
图片文字识别转换
国外域名注册商
使用免费虚拟主机
云电脑主机多少钱
鲲鹏云手机
短信验证码平台
OCR图片文字识别
SSL证书是什么
申请企业邮箱步骤
免费的企业用邮箱
云免流搭建教程
域名价格
推荐文章
ServerLess102