Git Product home page Git Product logo

image-java-sdk-v2.0's Introduction

腾讯云 智能图像服务 SDK for JAVA

使用前准备

  1. 前往注册: 腾讯云账号注册 (详细指引见 注册腾讯云
  2. 取得存储桶名称 BucketName: 请前往 创建存储桶
  3. 取得 APPIDSecretIdSecretKey:请前往 云API密钥 ,点击“新建密钥”

如何集成此 SDK 到你的项目中

使用 maven

<dependency>
  <groupId>com.qcloud</groupId>
  <artifactId>qcloud-image-sdk</artifactId>
  <version>2.3.6</version>
</dependency>

使用 Gradle

compile 'com.qcloud:qcloud-image-sdk:2.3.6'

使用 jar 文件

  1. jar 文件见 release/*-with-dependencies.jar
  2. 把 jar 文件导入到你的项目中

使用简介

初始化

ImageClient imageClient = new ImageClient(appId, secretId, secretKey, ImageClient.NEW_DOMAIN_recognition_image_myqcloud_com/*默认使用新域名, 如果你是老用户, 请选择旧域名*/);

设置代理

根据实际网络环境,可能要设置代理,例如:

Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("127.0.0.1", 8080));
imageClient.setProxy(proxy);

使用

SDK 提供功能如下:

图像识别:鉴黄,标签
文字识别(OCR):身份证,名片,通用,驾驶证行驶证,营业执照,银行卡,车牌号
人脸识别:人脸检测,五官定位,个体信息管理,人脸验证,人脸对比及人脸检索
人脸核身:照片核身(通过照片和身份证信息),获取唇语验证码(用于活体核身),活体核身(通过视频和照片),活体核身(通过视频和身份证信息)

// 调用车牌识别API示例
String imageUrl = "http://youtu.qq.com/app/img/experience/char_general/icon_ocr_license_3.jpg";
String result = imageClient.ocrPlate(new OcrPlateRequest("bucketName", imageUrl));
System.out.println(result);

更多例子详情可参见 Demo.java 的代码。

如何运行这个 Demo 工程

填写必要信息

修改文件 src/main/java/com/qcloud/image/demo/Demo.java 的 main() 方法,填入本文开头所述的 APPIDSecretIdSecretKeyBucketName

导入 Intellij IEDA

  1. Import Project -> 选择工程目录 -> Import project from external model -> Maven
  2. 运行:Demo.java 右键,Run Demo.main()

导入 Eclipse

  1. Eclipse 需要先安装 maven 插件: 点击 Eclipse 菜单栏 Help -> Eclipse Marketplace, 搜索框 Find 中输入 maven, 点击右侧的 Go, 搜索到插件 Maven Integration for Eclipse, 安装之.
  2. Eclipse 菜单栏 File -> Import… -> Maven -> Existing Maven Project -> 选择工程目录 -> 点击 Finish.
  3. 运行: Eclipse 菜单栏 Run -> Run.

如何修改源码

  1. 把工程源码导入到 IDE 中, 如上面 [如何运行这个 Demo 工程] 所述
  2. 修改源码
  3. 在工程根目录下执行命令 mvn assembly:assembly,编译结果见 target/*-with-dependencies.jar
  4. 在你的项目中取消对本 SDK 的依赖, 并导入编译所得的 jar 文件

image-java-sdk-v2.0's People

Contributors

fantouch avatar serenazlk avatar wmiao1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

image-java-sdk-v2.0's Issues

关于使用时的问题

demo中有设置是否使用https的方法,使用maven导入的包中没有此方法,如何解决呢

通过视频对比指定身份信息接口 报异常ContentTooLongException

FaceIdCardLiveDetectFourRequest liveDetectReq = new FaceIdCardLiveDetectFourRequest(bucketName, validate, video, liveDetectIdcardNumber, liveDetectIdcardName, seq);
ret = imageClient.faceIdCardLiveDetectFour(liveDetectReq);

Exception:org.apache.http.ContentTooLongException: Content length is too long: 2277591"}

调用这个方法报一下异常。如何解决

直接使用release 的 jar 集成到react-native 提示重复引用class

Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: 
duplicate entry: com/qcloud/image/ClientConfig.class

总是报上面类似的错误。react-native中没有使用其它库,是纯净的。

请问如何解决呢。google了一番,也没找到方法。

dependencies {
    compile 'com.facebook.react:react-native:+'
    compile files('libs/qcloud-sdk-2.2.7-jar-with-dependencies.jar')
}

人脸检索时候使用FaceIdentifyRequest采用byte[]作为参数时报错

封装的腾讯sdk接口

调用FaceIdentifyRequest并传入的是byte的图片文件时

 /**
     * 人脸检索 byte模式
     * @param image
     * @param groupIds
     * @return
     * @throws AbstractImageException
     * @throws IOException
     */
private QcloudFaceResult<IdentifyResult> faceIdentifyByte( byte[] image, String[] groupIds) throws AbstractImageException, IOException {

        FaceIdentifyRequest request = new FaceIdentifyRequest(CosBucketNameConst.bucketName, groupIds, image);

        String ret = imageClient.faceIdentify(request);

        return JsonUtil.json2pojo(ret, QcloudFaceResult.class, IdentifyResult.class);
}

定位到com.qcloud.image.op.DetectionOp这个类下,发现了未对byte[]类型的文件进行判断(url以及file形式的参数进行了判断)

public String faceIdentify(FaceIdentifyRequest request) throws AbstractImageException {
        request.check_param();
        String sign = Sign.appSign(this.cred, request.getBucketName(), (long)this.config.getSignExpired());
        String url = "https://" + this.config.getQCloudImageDomain() + this.config.getFaceIdentify();
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setUrl(url);
        httpRequest.addHeader("Authorization", sign);
        httpRequest.addHeader("User-Agent", this.config.getUserAgent());
        httpRequest.addParam("appid", String.valueOf(this.cred.getAppId()));
        httpRequest.addParam("bucket", request.getBucketName());
        httpRequest.setMethod(HttpMethod.POST);
        String groupId = request.getGroupId();
        String[] groupIds = request.getGroupIds();
        if (request.isUrl()) {  //对url进行
            httpRequest.addHeader("Content-Type", String.valueOf(HttpContentType.APPLICATION_JSON));
            httpRequest.addParam("url", request.getUrl());
            httpRequest.setContentType(HttpContentType.APPLICATION_JSON);
            if (groupId != null && !groupId.isEmpty()) {
                httpRequest.addParam("group_id", groupId);
            } else {
                if (groupIds == null || groupIds.length <= 0) {
                    throw new ParamException("groupId and groupIds both null or empty!!");
                }

                httpRequest.addParam("group_ids", request.getGroupIds());
            }
        } else { 
            httpRequest.setContentType(HttpContentType.MULTIPART_FORM_DATA);
            httpRequest.addFile("image", request.getImage()); //这个位置不管传入的是byte还是file都addFile,若注入的是byte[]参数会引发后面一个错误
            BytesContent bytesContent = request.getBytesContent();
            if (bytesContent != null) {
                httpRequest.addBytes(bytesContent.getKey(), bytesContent.getContent());
            }

            if (groupId != null && !groupId.isEmpty()) {
                httpRequest.addParam("group_id", groupId);
            } else {
                if (groupIds == null || groupIds.length <= 0) {
                    throw new ParamException("groupId and groupIds both null or empty!!");
                }

                for(int index = 0; index < groupIds.length; ++index) {
                    String key = String.format("group_ids[%d]", index);
                    String data = groupIds[index];
                    httpRequest.addParam(key, data);
                }
            }
        }

        return this.httpClient.sendHttpRequest(httpRequest);
    }

错误定位到com.qcloud.image.http.DefaultImageHttpClient 这个位置的sendPostRequest方法,这里把空的file传入到一个hashmap中,并作为参数设值到了httpRequest中,执行httpClient.sendHttpRequest(httpRequest)这个方法进入到com.qcloud.image.http.AbstractImageHttpClient这个类

执行

public String sendHttpRequest(HttpRequest httpRequest) throws AbstractImageException {

        HttpMethod method = httpRequest.getMethod();
        if (method == HttpMethod.POST) {
            return sendPostRequest(httpRequest);//进入这个方法
        } else {
            throw new ParamException("Unsupported Http Method");
        }
    }

执行sendPostRequest(httpRequest)定位到com.qcloud.image.http.DefaultImageHttpClient这个类

在这个类中,因为前面hashmap传入了一个空的file,导致size>0,并引发了

if (file == null) {
                    throw new FileNotFoundException("File is null: " + key);
                }

这个错误。

private void setMultiPartEntity(MultipartBuilder multipartBuilder, Map<String, Object> params, Map<String, File> files, Map<String, byte[]> fileContents)
            throws FileNotFoundException {

        multipartBuilder.type(MultipartBuilder.FORM);
        for (String paramKey : params.keySet()) {
            multipartBuilder.addFormDataPart(paramKey, String.valueOf(params.get(paramKey)));
        }

        if (files.size() > 0) {
            for (String key : files.keySet()) {
                File file = files.get(key);
                if (file == null) {
                    throw new FileNotFoundException("File is null: " + key);
                }
                if (!file.exists()) {
                    throw new FileNotFoundException("File Not Exists: " + file.getAbsolutePath());
                }
                multipartBuilder.addPart(
                        Headers.of("Content-Disposition", String.format("form-data; name=\"%s\"; filename=\"%s\"", key, file.getName())),
                        RequestBody.create(MediaType.parse("image/jpg"), file)
                );
            }
        }

        for (String key : fileContents.keySet()) {
            byte[] content = fileContents.get(key);
            if (content != null && content.length > 0) {
                multipartBuilder.addPart(
                        //TODO file name not resolved
                        Headers.of("Content-Disposition", String.format("form-data; name=\"%s\"; filename=\"%s\"", key, "bytes" + System.currentTimeMillis())),
                        RequestBody.create(MediaType.parse("image/jpg"), content)
                );
            }
        }
        
    }

@@以上就是遇到的问题,还希望能把后台界面完善下,我上传person的信息,还有我组的相关信息,但是后台都看不到,这个问题很蛋疼,麻烦能关注下这个点。

身份证识别-1408

/**
     * 身份证识别请求
     * 
     * @param request 标签识别请求参数
     * @return JSON格式的字符串, 格式为{"code":$code, "message":"$mess"}, code为0表示成功, 其他为失败,
     *         message为success或者失败原因
     * @throws AbstractImageException SDK定义的Image异常, 通常是输入参数有误或者环境问题(如网络不通)
     */  
    public String idcardDetect(IdcardDetectRequest request) throws AbstractImageException {
        request.check_param();
        String sign = Sign.appSign(cred, request.getBucketName(), this.config.getSignExpired());
        String url = "http://" + this.config.getQCloudImageDomain() + this.config.getDetectionIdcard();
        
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setUrl(url);
        httpRequest.addHeader(RequestHeaderKey.Authorization, sign);
        httpRequest.addHeader(RequestHeaderKey.USER_AGENT, this.config.getUserAgent());

        httpRequest.addParam(RequestBodyKey.APPID, String.valueOf(cred.getAppId()));
        httpRequest.addParam(RequestBodyKey.BUCKET, request.getBucketName());
        httpRequest.addParam(RequestBodyKey.CARD_TYPE, String.valueOf(request.getCardType()));
        
        httpRequest.setMethod(HttpMethod.POST);
        if (request.isUrl()) {
            httpRequest.setContentType(HttpContentType.APPLICATION_JSON); 
            httpRequest.addParam(RequestBodyKey.URL_LIST, (request.getUrlList())); 
        } else {
            httpRequest.setContentType(HttpContentType.MULTIPART_FORM_DATA);
            HashMap<String, String> keys = request.getKeyList();
            HashMap<String, File> images = request.getImageList();
            for (String k : keys.keySet()) {
                httpRequest.addFile(keys.get(k), images.get(k));
            }
        }
              
        return httpClient.sendHttpRequest(httpRequest);
    }

this.config.getQCloudImageDomain() 是不是应该换成 this.config.getQCloudOcrDomain()

建议添加可自定义referer

由于ocr 图片识别多涉及隐私信息,故图片多做了referer保护,但是sdk里面header是写死的,没办法通过配置进行修改,建议修改成可以配置header或可配置referer

调用驾驶证识别接口报错:code":-3 Caused by: java.net.UnknownHostException

报错: {"code":-3,"message":"杩欐槸鍦ㄤ富鏈哄悕瑙f瀽鏃堕�氬父鍑虹幇鐨勬殏鏃堕敊璇紝瀹冩剰鍛崇潃鏈湴鏈嶅姟鍣ㄦ病鏈変粠鏉冨▉鏈嶅姟鍣ㄤ笂鏀跺埌鍝嶅簲銆� (recognition.image.myqcloud.com)"}
Caused by: java.net.UnknownHostException: 杩欐槸鍦ㄤ富鏈哄悕瑙f瀽鏃堕�氬父鍑虹幇鐨勬殏鏃堕敊璇紝瀹冩剰鍛崇潃鏈湴鏈嶅姟鍣ㄦ病鏈変粠鏉冨▉鏈嶅姟鍣ㄤ笂鏀跺埌鍝嶅簲銆� (recognition.image.myqcloud.com)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
at java.net.InetAddress.getAllByName(InetAddress.java:1192)
at java.net.InetAddress.getAllByName(InetAddress.java:1126)
at com.squareup.okhttp.Dns$1.lookup(Dns.java:39)
at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.squareup.okhttp.Call.getResponse(Call.java:286)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
at com.squareup.okhttp.Call.execute(Call.java:80)
at com.qcloud.image.http.DefaultImageHttpClient.sendPostRequest(DefaultImageHttpClient.java:117)

public static void main(String args[]){
ImageClient imageClient = new ImageClient("AAAAAAA", "AAAAAAA", "AAAAAAA", ImageClient.NEW_DOMAIN_recognition_image_myqcloud_com);
try{
OcrDrivingLicenceRequest request = new OcrDrivingLicenceRequest("drive-license1", OcrDrivingLicenceRequest.TYPE_DRIVER_LICENSE, "http://drive-license1-1258218565.picgz.myqcloud.com/timg2.jpg");
String result = imageClient.ocrDrivingLicence(request);
System.out.println(result );
}catch(Exception e){
e.printStackTrace();
}
}

人脸静态活体检测失败

请求都是返回该JSON字符串: {"code":0,"message":"OK","data":{"session_id":"not_set_session_id","score":0}},我看人脸静态活体检测文档里面没有对 not_set_session_id 进行说明啊

android使用报错

android studio 中 使用车牌号码识别报错
java.lang.NoSuchFieldError: org.apache.http.conn.ssl.AllowAllHostnameVerifier.INSTANCE

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.