阿里云图像识别Java调用示例参考

阿里云图像识别Java调用示例参考

简介: 本文给出使用Java语言分别使用网络图片和本地图片调用场景识别API的示例。

概述

图像识别服务(Image Recognition)基于大数据和深度学习实现,可精准识别图像中的视觉内容,包括上千种物体标签、数十种常见场景等,包含场景分类、图像打标、鉴黄等在线API服务模块,应用于智能相册管理、图片分类和检索、图片安全监控等场景。下面给出使用Java语言分别使用网络图片和本地图片调用场景识别API的示例。

控制台测试

与人脸识别类似,可以参考:阿里云人脸识别使用流程简介 。

Java Code Sample

使用网络图片测试

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;
//@SuppressWarnings(“restriction”)
public class ComputerDemo1 {

/*

  • 计算MD5+BASE64
    /
    public static String MD5Base64(String s) {
    if (s == null)
    return null;
    String encodeStr = “”;
    byte[] utfBytes = s.getBytes();
    MessageDigest mdTemp;
    try {
    mdTemp = MessageDigest.getInstance(“MD5”);
    mdTemp.update(utfBytes);
    byte[] md5Bytes = mdTemp.digest();
    BASE64Encoder b64Encoder = new BASE64Encoder();
    encodeStr = b64Encoder.encode(md5Bytes);
    } catch (Exception e) {
    throw new Error("Failed to generate MD5 : " + e.getMessage());
    }
    return encodeStr;
    }
    /
  • 计算 HMAC-SHA1
    /
    public static String HMACSha1(String data, String key) {
    String result;
    try {
    SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), “HmacSHA1”);
    Mac mac = Mac.getInstance(“HmacSHA1”);
    mac.init(signingKey);
    byte[] rawHmac = mac.doFinal(data.getBytes());
    result = (new BASE64Encoder()).encode(rawHmac);
    } catch (Exception e) {
    throw new Error("Failed to generate HMAC : " + e.getMessage());
    }
    return result;
    }
    /
  • 等同于javaScript中的 new Date().toUTCString();
    /
    public static String toGMTString(Date date) {
    SimpleDateFormat df = new SimpleDateFormat(“E, dd MMM yyyy HH:mm:ss z”, Locale.UK);
    df.setTimeZone(new java.util.SimpleTimeZone(0, “GMT”));
    return df.format(date);
    }
    /
  • 发送POST请求
    /
    public static String sendPost(String url, String body, String ak_id, String ak_secret) throws Exception {
    PrintWriter out = null;
    BufferedReader in = null;
    String result = “”;
    int statusCode = 200;
    try {
    URL realUrl = new URL(url);
    /

    * http header 参数
    /
    String method = “POST”;
    String accept = “application/json”;
    String content_type = “application/json”;
    String path = realUrl.getFile();
    String date = toGMTString(new Date());
    // 1.对body做MD5+BASE64加密
    String bodyMd5 = MD5Base64(body);
    String stringToSign = method + “\n” + accept + “\n” + bodyMd5 + “\n” + content_type + “\n” + date + “\n”
    + path;
    // 2.计算 HMAC-SHA1
    String signature = HMACSha1(stringToSign, ak_secret);
    // 3.得到 authorization header
    String authHeader = “Dataplus " + ak_id + “:” + signature;
    // 打开和URL之间的连接
    URLConnection conn = realUrl.openConnection();
    // 设置通用的请求属性
    conn.setRequestProperty(“accept”, accept);
    conn.setRequestProperty(“content-type”, content_type);
    conn.setRequestProperty(“date”, date);
    conn.setRequestProperty(“Authorization”, authHeader);
    // 发送POST请求必须设置如下两行
    conn.setDoOutput(true);
    conn.setDoInput(true);
    // 获取URLConnection对象对应的输出流
    out = new PrintWriter(conn.getOutputStream());
    // 发送请求参数
    out.print(body);
    // flush输出流的缓冲
    out.flush();
    // 定义BufferedReader输入流来读取URL的响应
    statusCode = ((HttpURLConnection)conn).getResponseCode();
    if(statusCode != 200) {
    in = new BufferedReader(new InputStreamReader(((HttpURLConnection)conn).getErrorStream()));
    } else {
    in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    }
    String line;
    while ((line = in.readLine()) != null) {
    result += line;
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    if (out != null) {
    out.close();
    }
    if (in != null) {
    in.close();
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    if (statusCode != 200) {
    throw new IOException(”\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
    }
    return result;
    }
    /
  • GET请求
    /
    public static String sendGet(String url, String ak_id, String ak_secret) throws Exception {
    String result = “”;
    BufferedReader in = null;
    int statusCode = 200;
    try {
    URL realUrl = new URL(url);
    /

    * http header 参数
    */
    String method = “GET”;
    String accept = “application/json”;
    String content_type = “application/json”;
    String path = realUrl.getFile();
    String date = toGMTString(new Date());
    // 1.对body做MD5+BASE64加密
    // String bodyMd5 = MD5Base64(body);
    String stringToSign = method + “\n” + accept + “\n” + “” + “\n” + content_type + “\n” + date + “\n” + path;
    // 2.计算 HMAC-SHA1
    String signature = HMACSha1(stringToSign, ak_secret);
    // 3.得到 authorization header
    String authHeader = “Dataplus " + ak_id + “:” + signature;
    // 打开和URL之间的连接
    URLConnection connection = realUrl.openConnection();
    // 设置通用的请求属性
    connection.setRequestProperty(“accept”, accept);
    connection.setRequestProperty(“content-type”, content_type);
    connection.setRequestProperty(“date”, date);
    connection.setRequestProperty(“Authorization”, authHeader);
    connection.setRequestProperty(“Connection”, “keep-alive”);
    // 建立实际的连接
    connection.connect();
    // 定义 BufferedReader输入流来读取URL的响应
    statusCode = ((HttpURLConnection)connection).getResponseCode();
    if(statusCode != 200) {
    in = new BufferedReader(new InputStreamReader(((HttpURLConnection)connection).getErrorStream()));
    } else {
    in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    }
    String line;
    while ((line = in.readLine()) != null) {
    result += line;
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    if (in != null) {
    in.close();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    if (statusCode != 200) {
    throw new IOException(”\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
    }
    return result;
    }
    public static void main(String[] args) throws Exception {
    // 发送POST请求示例
    String ak_id = "; //用户ak
    String ak_secret = "”; // 用户ak_secret
    String url = “https://dtplus-cn-shanghai.data.aliyuncs.com/image/scene”;
    String body = “{“type”: “0”, “image_url”:“https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1544608444&di=54513735567736d0eaeba7a13aa0421c&src=http://img5q.duitang.com/uploads/item/201411/23/20141123205812_Bircn.jpeg”}”;
    System.out.println(“response body:” + sendPost(url, body, ak_id, ak_secret));

}
}

使用本地图片测试

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;
public class ComputerDemo2 {

/*

  • 计算MD5+BASE64
    /
    public static String MD5Base64(String s) {
    if (s == null)
    return null;
    String encodeStr = “”;
    byte[] utfBytes = s.getBytes();
    MessageDigest mdTemp;
    try {
    mdTemp = MessageDigest.getInstance(“MD5”);
    mdTemp.update(utfBytes);
    byte[] md5Bytes = mdTemp.digest();
    BASE64Encoder b64Encoder = new BASE64Encoder();
    encodeStr = b64Encoder.encode(md5Bytes);
    } catch (Exception e) {
    throw new Error("Failed to generate MD5 : " + e.getMessage());
    }
    return encodeStr;
    }
    /

  • 计算 HMAC-SHA1
    /
    public static String HMACSha1(String data, String key) {
    String result;
    try {
    SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), “HmacSHA1”);
    Mac mac = Mac.getInstance(“HmacSHA1”);
    mac.init(signingKey);
    byte[] rawHmac = mac.doFinal(data.getBytes());
    result = (new BASE64Encoder()).encode(rawHmac);
    } catch (Exception e) {
    throw new Error("Failed to generate HMAC : " + e.getMessage());
    }
    return result;
    }
    /

  • 等同于javaScript中的 new Date().toUTCString();
    /
    public static String toGMTString(Date date) {
    SimpleDateFormat df = new SimpleDateFormat(“E, dd MMM yyyy HH:mm:ss z”, Locale.UK);
    df.setTimeZone(new java.util.SimpleTimeZone(0, “GMT”));
    return df.format(date);
    }
    /

  • 发送POST请求
    /
    public static String sendPost(String url, String body, String ak_id, String ak_secret) throws Exception {
    PrintWriter out = null;
    BufferedReader in = null;
    String result = “”;
    int statusCode = 200;
    try {
    URL realUrl = new URL(url);
    /

    * http header 参数
    /
    String method = “POST”;
    String accept = “application/json”;
    String content_type = “application/json”;
    String path = realUrl.getFile();
    String date = toGMTString(new Date());
    // 1.对body做MD5+BASE64加密
    String bodyMd5 = MD5Base64(body);
    String stringToSign = method + “\n” + accept + “\n” + bodyMd5 + “\n” + content_type + “\n” + date + “\n”
    + path;
    // 2.计算 HMAC-SHA1
    String signature = HMACSha1(stringToSign, ak_secret);
    // 3.得到 authorization header
    String authHeader = “Dataplus " + ak_id + “:” + signature;
    // 打开和URL之间的连接
    URLConnection conn = realUrl.openConnection();
    // 设置通用的请求属性
    conn.setRequestProperty(“accept”, accept);
    conn.setRequestProperty(“content-type”, content_type);
    conn.setRequestProperty(“date”, date);
    conn.setRequestProperty(“Authorization”, authHeader);
    // 发送POST请求必须设置如下两行
    conn.setDoOutput(true);
    conn.setDoInput(true);
    // 获取URLConnection对象对应的输出流
    out = new PrintWriter(conn.getOutputStream());
    // 发送请求参数
    out.print(body);
    // flush输出流的缓冲
    out.flush();
    // 定义BufferedReader输入流来读取URL的响应
    statusCode = ((HttpURLConnection)conn).getResponseCode();
    if(statusCode != 200) {
    in = new BufferedReader(new InputStreamReader(((HttpURLConnection)conn).getErrorStream()));
    } else {
    in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    }
    String line;
    while ((line = in.readLine()) != null) {
    result += line;
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    if (out != null) {
    out.close();
    }
    if (in != null) {
    in.close();
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    if (statusCode != 200) {
    throw new IOException(”\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
    }
    return result;
    }
    /

  • GET请求
    /
    public static String sendGet(String url, String ak_id, String ak_secret) throws Exception {
    String result = “”;
    BufferedReader in = null;
    int statusCode = 200;
    try {
    URL realUrl = new URL(url);
    /

    * http header 参数
    */
    String method = “GET”;
    String accept = “application/json”;
    String content_type = “application/json”;
    String path = realUrl.getFile();
    String date = toGMTString(new Date());
    // 1.对body做MD5+BASE64加密
    // String bodyMd5 = MD5Base64(body);
    String stringToSign = method + “\n” + accept + “\n” + “” + “\n” + content_type + “\n” + date + “\n” + path;
    // 2.计算 HMAC-SHA1
    String signature = HMACSha1(stringToSign, ak_secret);
    // 3.得到 authorization header
    String authHeader = “Dataplus " + ak_id + “:” + signature;
    // 打开和URL之间的连接
    URLConnection connection = realUrl.openConnection();
    // 设置通用的请求属性
    connection.setRequestProperty(“accept”, accept);
    connection.setRequestProperty(“content-type”, content_type);
    connection.setRequestProperty(“date”, date);
    connection.setRequestProperty(“Authorization”, authHeader);
    connection.setRequestProperty(“Connection”, “keep-alive”);
    // 建立实际的连接
    connection.connect();
    // 定义 BufferedReader输入流来读取URL的响应
    statusCode = ((HttpURLConnection)connection).getResponseCode();
    if(statusCode != 200) {
    in = new BufferedReader(new InputStreamReader(((HttpURLConnection)connection).getErrorStream()));
    } else {
    in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    }
    String line;
    while ((line = in.readLine()) != null) {
    result += line;
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    if (in != null) {
    in.close();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    if (statusCode != 200) {
    throw new IOException(”\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
    }
    return result;
    }
    public static void main(String[] args) throws Exception {
    // 发送POST请求示例
    String ak_id = "; //用户ak
    String ak_secret = "”; // 用户ak_secret
    String url = “https://dtplus-cn-shanghai.data.aliyuncs.com/image/scene”;
    //上传本地图片
    // Request body
    String pic_path = “C:\Users\taro.yt\Desktop\timg.jpg”;

    File picBase64 = new File(pic_path);
    String pic = encodeImageToBase64(picBase64);

    //提出base64编码的换行符问题
    String data = pic.replaceAll("[\s*\t\n\r]", “”);
    data = “’” + data + “’”;

    String body = “{“type”: “1”, “content”:”+data+"}";

    System.out.println(“response body:” + sendPost(url, body, ak_id, ak_secret));
    }
    /**

  • 将本地图片编码为base64

  • @param file

  • @return

  • @throws Exception
    */
    public static String encodeImageToBase64(File file) throws Exception {
    //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
    // loggerger.info(“图片的路径为:” + file.getAbsolutePath());

    InputStream in = null;
    byte[] data = null;
    //读取图片字节数组
    try {
    in = new FileInputStream(file);
    data = new byte[in.available()];
    in.read(data);
    in.close();
    } catch (IOException e) {
    e.printStackTrace();
    throw new Exception(“图片上传失败,请联系客服!”);
    }
    //对字节数组Base64编码
    BASE64Encoder encoder = new BASE64Encoder();
    String base64 = encoder.encode(data);
    return base64;//返回Base64编码过的字节数组字符串
    }
    }

测试结果

response body:{“tags”:[{“value”:“人物”,“confidence”:97.0},{“value”:“演出”,“confidence”:12.0},{“value”:“其他”,“confidence”:12.0},{“value”:“室内”,“confidence”:11.0}],“errno”:0,“request_id”:“b31aebed-305a-4377-a843-ac4e9d3d30c1”}

参考链接

场景识别API调用说明

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值