美文网首页
java与python 交互(调用API)

java与python 交互(调用API)

作者: gramthna | 来源:发表于2019-02-11 11:24 被阅读9次

    java与python 交互为两种情况:

        1:直接调用接口(url)

         2:调用二者的类

    其中第一种不过多赘述:

    #Author:morefree

    #-*- coding:utf-8-*-

    import cv2

    import requests

    import os

    import logging

    import glob

    def pic(i):

        pic_list = glob.glob('D:\\untitled\\book\\demovideo\\video{}.png'.format(i))

        print(pic_list)

        return pic_list

    def javaapi(filename):

        url=""

        data={"parentId":"","fileCategory":"personal","fileSize":179,"fileName":"video{}.png".format(filename),"uoType":1}

        #filesize:指的是文件的字节大小。 filename:指的是你上传之后保存的文件名

        # headers={''}

        files={'file':('video{}.png'.format(filename),open('D:\\untitled\\book\\demovideo\\video{}.png'.format(filename),'rb'),'image/png',{})}

        r=requests.post(url,data,files=files)

        print(r.text)

    LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"

    logging.basicConfig(filename='my.log', level=logging.DEBUG, format=LOG_FORMAT)

    # def save_pic(filepath="book\img"):

    #    if not os.path.exists(filepath):

    #        print("create filepath")

    #        os.makedirs(filepath)

    #        logging.debug("create a path")

    #        # filehouzhui = os.path.splitext(pic)[1]

    #        # filename = "{}\{}".format(filepath, file_name)

    #        #

    #        # urllib.request.urlretrieve(pic, filename=filename)

    path1='D:\week04c.mp4'#文件名

    vc=cv2.VideoCapture(path1)

    fps=vc.get(5)

    # print(fps)

    # print("count",vc.get(7))

    # print(type(vc))

    logging.info("the video can be read")

    count1=1

    success =True

    time1=1000

    try:

        while (success):

            success,frame=vc.read()

            i=1

            if (count1% time1 == 0):

                cv2.imwrite("video" + "%d.png" % (count1/1000), frame)

                logging.debug("save successfully")

                # print(count1/1000)

                pic(int(count1/1000))

                javaapi(int(count1/1000))

            # print(i)

            count1=count1+1

        vc.release()

    except:

        print("读取失败")

        logging.error("the video can not be read")

        # return pic_list

    #调用接口

    # url = ''

    # data = {'XXXX':'XXX'}

    # headers = {'Connection':'close'}

    # s=requests.session()

    # r = s.post(url,data = data,headers = headers)

    # # print(r.text.encode('utf-8'))

    # pic()

    # f={

    #    "localUrl":(None,'1.jpg'),

    #

    # }

    # r=s.post(url,files=f)

    第二种可以用到jython但是版本过于老旧,最近在研究接口的东西发现py4j,他可以完美解决jython的问题,well,首先安装(本人用windows7,anaconda)直接pip install py4j,就ok了,然后在anaconda中的share中找到py4j,以及目录下的jar,导入到eclipse中。一切ok

    接下来就是二者的交互了,不过多赘述。

    文章部分来源:https://blog.csdn.net/tryto21/article/details/79667107

    相关文章

      网友评论

          本文标题:java与python 交互(调用API)

          本文链接:https://www.haomeiwen.com/subject/apzfeqtx.html