美文网首页
腾讯的文字识别ocr的使用

腾讯的文字识别ocr的使用

作者: 名字_都被占了 | 来源:发表于2018-04-28 19:38 被阅读0次

    先到https://github.com/tencentyun/image-java-sdk-v2.0下载好对应的jar包

    代码如下:
    在CeShi.java文件中

    public class CeShi extends AppCompatActivity {
        private ImageClient imageClient;
        private TextView textView;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ceshi);
            textView = findViewById(R.id.textView2);
            imageClient = new ImageClient("你的APPID", "你的SecretId", "你的SecretKey");
            new Thread() {
                @Override
                public void run() {
                    ocrGeneral(imageClient, "tencentyun");
                }
            }.start();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            imageClient.shutdown();
        }
    
        private void ocrGeneral(ImageClient imageClient, String bucketName) {
            final String ret;
            GeneralOcrRequest request = new GeneralOcrRequest(bucketName, new File("/storage/emulated/0/ocr_common09.jpg"));
            ret = imageClient.generalOcr(request);
            Log.d("CeShi", ret);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Gson gson = new Gson();
                    OcrWenZiEntity ocrWenZiEntity = gson.fromJson(ret, OcrWenZiEntity.class);
                    StringBuffer stringBuffer = new StringBuffer();
                    for (OcrWenZiEntity.Datashiti.Itemsshiti itemsshiti : ocrWenZiEntity.getData().getItems()) {
                        stringBuffer.append(itemsshiti.getItemstring());
                    }
                    textView.setText(stringBuffer);
                    Log.d("CeShi", stringBuffer.toString());
                    //输出结果为:产品特点1.本产品精选优质纯棉织造(装饰部分除外)。2.手感柔软、毛圈蓬松、吸水性好,接触肌肤感觉自然舒适。3.染色全部使用进口染料,色牢度好、耐洗涤。温馨提示1.深色产品初次洗涤时会有轻微掉色现象,割绒产品不宜机洗。2.产品不宜长期处于潮湿状态,不宜长时间曝晒。3.不宜与化纤织物同时洗涤。
                    try {
                        FileOutputStream fileOutputStream = new FileOutputStream("/storage/emulated/0/shuchu.txt");
                        try {
                            fileOutputStream.write(stringBuffer.toString().getBytes());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    

    在OcrWenZiEntity.java文件中

    public class OcrWenZiEntity {
    
        /**
         * code : 0
         * message : OK
         * data : {"class":[],"angle":0,"items":[{"itemcoord":{"x":320,"y":171,"width":95,"height":24},"words":[{"character":"产","confidence":0.9999980926513672},{"character":"品","confidence":0.9999918937683105},{"character":"特","confidence":0.9999916553497316},{"character":"点","confidence":0.9999955892562866}],"itemstring":"产品特点"},{"itemcoord":{"x":233,"y":224,"width":276,"height":19},"words":[{"character":"1","confidence":0.9990590214729308},{"character":".","confidence":0.998279333114624},{"character":"本","confidence":0.9998663663864136},{"character":"产","confidence":0.9999933242797852},{"character":"品","confidence":0.9999903440475464},{"character":"精","confidence":0.9998431205749512},{"character":"选","confidence":0.9999943971633912},{"character":"优","confidence":0.9997697472572328},{"character":"质","confidence":0.9999964237213136},{"character":"纯","confidence":0.9998756647109984},{"character":"棉","confidence":0.9999834299087524},{"character":"织","confidence":0.9999822378158568},{"character":"造","confidence":0.9999788999557496},{"character":"(","confidence":0.999357044696808},{"character":"装","confidence":0.9972469806671144},{"character":"饰","confidence":0.9999996423721313},{"character":"部","confidence":0.999953269958496},{"character":"分","confidence":0.9999784231185912}],"itemstring":"1.本产品精选优质纯棉织造(装饰部分"},{"itemcoord":{"x":248,"y":249,"width":56,"height":18},"words":[{"character":"除","confidence":0.9999402761459352},{"character":"外","confidence":0.999816358089447},{"character":")","confidence":0.9993292093276978},{"character":"。","confidence":0.9982624650001526}],"itemstring":"除外)。"},{"itemcoord":{"x":233,"y":277,"width":287,"height":18},"words":[{"character":"2","confidence":0.99992036819458},{"character":".","confidence":0.9986629486083984},{"character":"手","confidence":0.9997488856315612},{"character":"感","confidence":0.9999783039093018},{"character":"柔","confidence":0.9998743534088136},{"character":"软","confidence":0.9998101592063904},{"character":"、","confidence":0.9987398982048036},{"character":"毛","confidence":0.9999630451202391},{"character":"圈","confidence":0.9999736547470092},{"character":"蓬","confidence":0.9898577332496644},{"character":"松","confidence":0.9999889135360718},{"character":"、","confidence":0.9989535808563232},{"character":"吸","confidence":0.999909281730652},{"character":"水","confidence":0.999908685684204},{"character":"性","confidence":0.9999761581420898},{"character":"好","confidence":0.999988317489624},{"character":",","confidence":0.9971440434455872},{"character":"接","confidence":0.999951958656311}],"itemstring":"2.手感柔软、毛圈蓬松、吸水性好,接"},{"itemcoord":{"x":248,"y":303,"width":153,"height":17},"words":[{"character":"触","confidence":0.9999226331710817},{"character":"肌","confidence":0.9999467134475708},{"character":"肤","confidence":0.9998550415039064},{"character":"感","confidence":0.9998602867126464},{"character":"觉","confidence":0.9978869557380676},{"character":"自","confidence":0.999998927116394},{"character":"然","confidence":1},{"character":"舒","confidence":0.9999998807907104},{"character":"适","confidence":0.999744951725006},{"character":"。","confidence":0.998598039150238}],"itemstring":"触肌肤感觉自然舒适。"},{"itemcoord":{"x":236,"y":329,"width":265,"height":19},"words":[{"character":"3","confidence":0.999930739402771},{"character":".","confidence":0.9979470372200012},{"character":"染","confidence":0.9999672174453736},{"character":"色","confidence":0.99997079372406},{"character":"全","confidence":0.9999613761901855},{"character":"部","confidence":0.9999263286590576},{"character":"使","confidence":0.999990940093994},{"character":"用","confidence":0.9999885559082032},{"character":"进","confidence":0.9996850490570068},{"character":"口","confidence":0.9999228715896606},{"character":"染","confidence":0.9999171495437622},{"character":"料","confidence":0.9999735355377196},{"character":",","confidence":0.994210958480835},{"character":"色","confidence":0.9999107122421264},{"character":"牢","confidence":0.9999504089355468},{"character":"度","confidence":0.999986171722412},{"character":"好","confidence":0.9999781847000122},{"character":"、","confidence":0.99902081489563}],"itemstring":"3.染色全部使用进口染料,色牢度好、"},{"itemcoord":{"x":247,"y":357,"width":57,"height":16},"words":[{"character":"耐","confidence":0.9995765089988708},{"character":"洗","confidence":0.997870683670044},{"character":"涤","confidence":0.9997075200080872},{"character":"。","confidence":0.999325156211853}],"itemstring":"耐洗涤。"},{"itemcoord":{"x":323,"y":403,"width":98,"height":25},"words":[{"character":"温","confidence":0.9985106587409972},{"character":"馨","confidence":0.9988638162612916},{"character":"提","confidence":0.9998421669006348},{"character":"示","confidence":0.9999939203262328}],"itemstring":"温馨提示"},{"itemcoord":{"x":235,"y":465,"width":280,"height":18},"words":[{"character":"1","confidence":0.9974324107170104},{"character":".","confidence":0.9943867921829224},{"character":"深","confidence":0.9999585151672364},{"character":"色","confidence":0.9999676942825316},{"character":"产","confidence":0.9999828338623048},{"character":"品","confidence":0.9999972581863404},{"character":"初","confidence":0.9999632835388184},{"character":"次","confidence":0.9999954700469972},{"character":"洗","confidence":0.9973767995834352},{"character":"涤","confidence":0.9998528957366944},{"character":"时","confidence":0.999848246574402},{"character":"会","confidence":0.9999499320983888},{"character":"有","confidence":0.9999771118164064},{"character":"轻","confidence":0.999948501586914},{"character":"微","confidence":0.9998568296432496},{"character":"掉","confidence":0.999832272529602},{"character":"色","confidence":0.9998605251312256},{"character":"现","confidence":0.9996801614761353}],"itemstring":"1.深色产品初次洗涤时会有轻微掉色现"},{"itemcoord":{"x":245,"y":492,"width":175,"height":18},"words":[{"character":"象","confidence":0.9999597072601318},{"character":",","confidence":0.9959152340888976},{"character":"割","confidence":0.999987006187439},{"character":"绒","confidence":0.9997709393501282},{"character":"产","confidence":0.9999940395355223},{"character":"品","confidence":0.9546661972999572},{"character":"不","confidence":0.9999688863754272},{"character":"宜","confidence":0.9999117851257324},{"character":"机","confidence":0.9999151229858398},{"character":"洗","confidence":0.9985805749893188},{"character":"。","confidence":0.999201238155365}],"itemstring":"象,割绒产品不宜机洗。"},{"itemcoord":{"x":227,"y":520,"width":289,"height":17},"words":[{"character":"2","confidence":0.9997889399528505},{"character":".","confidence":0.994099497795105},{"character":"产","confidence":0.9999057054519652},{"character":"品","confidence":0.9999979734420776},{"character":"不","confidence":0.999951958656311},{"character":"宜","confidence":0.9996045231819152},{"character":"长","confidence":0.9999979734420776},{"character":"期","confidence":0.9999982118606568},{"character":"处","confidence":0.9999972581863404},{"character":"于","confidence":0.9988164901733398},{"character":"潮","confidence":0.999860405921936},{"character":"湿","confidence":0.9999277591705322},{"character":"状","confidence":0.9999464750289916},{"character":"态","confidence":0.9999442100524902},{"character":",","confidence":0.9985288381576538},{"character":"不","confidence":0.9999818801879884},{"character":"宜","confidence":0.9999547004699708},{"character":"长","confidence":0.9999994039535524}],"itemstring":"2.产品不宜长期处于潮湿状态,不宜长"},{"itemcoord":{"x":244,"y":548,"width":73,"height":18},"words":[{"character":"时","confidence":0.9998537302017212},{"character":"间","confidence":0.9999980926513672},{"character":"曝","confidence":0.9999419450759888},{"character":"晒","confidence":0.9997050166130066},{"character":"。","confidence":0.9992271661758424}],"itemstring":"时间曝晒。"},{"itemcoord":{"x":225,"y":576,"width":212,"height":19},"words":[{"character":"3","confidence":0.999473512172699},{"character":".","confidence":0.9980056881904602},{"character":"不","confidence":0.999929428100586},{"character":"宜","confidence":0.999871015548706},{"character":"与","confidence":0.9999983310699464},{"character":"化","confidence":0.9999493360519408},{"character":"纤","confidence":0.999975085258484},{"character":"织","confidence":0.999985694885254},{"character":"物","confidence":0.9999982118606568},{"character":"同","confidence":0.9999945163726808},{"character":"时","confidence":0.9999511241912842},{"character":"洗","confidence":0.9978109002113342},{"character":"涤","confidence":0.9990622401237488},{"character":"。","confidence":0.9989842772483826}],"itemstring":"3.不宜与化纤织物同时洗涤。"}],"session_id":"1255122536976413777"}
         */
    
        private int code;
        private String message;
        private Datashiti data;
    
        public int getCode() {
            return code;
        }
    
        public void setCode(int code) {
            this.code = code;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public Datashiti getData() {
            return data;
        }
    
        public void setData(Datashiti data) {
            this.data = data;
        }
    
        public static class Datashiti {
            /**
             * class : []
             * angle : 0.0
             * items : [{"itemcoord":{"x":320,"y":171,"width":95,"height":24},"words":[{"character":"产","confidence":0.9999980926513672},{"character":"品","confidence":0.9999918937683105},{"character":"特","confidence":0.9999916553497316},{"character":"点","confidence":0.9999955892562866}],"itemstring":"产品特点"},{"itemcoord":{"x":233,"y":224,"width":276,"height":19},"words":[{"character":"1","confidence":0.9990590214729308},{"character":".","confidence":0.998279333114624},{"character":"本","confidence":0.9998663663864136},{"character":"产","confidence":0.9999933242797852},{"character":"品","confidence":0.9999903440475464},{"character":"精","confidence":0.9998431205749512},{"character":"选","confidence":0.9999943971633912},{"character":"优","confidence":0.9997697472572328},{"character":"质","confidence":0.9999964237213136},{"character":"纯","confidence":0.9998756647109984},{"character":"棉","confidence":0.9999834299087524},{"character":"织","confidence":0.9999822378158568},{"character":"造","confidence":0.9999788999557496},{"character":"(","confidence":0.999357044696808},{"character":"装","confidence":0.9972469806671144},{"character":"饰","confidence":0.9999996423721313},{"character":"部","confidence":0.999953269958496},{"character":"分","confidence":0.9999784231185912}],"itemstring":"1.本产品精选优质纯棉织造(装饰部分"},{"itemcoord":{"x":248,"y":249,"width":56,"height":18},"words":[{"character":"除","confidence":0.9999402761459352},{"character":"外","confidence":0.999816358089447},{"character":")","confidence":0.9993292093276978},{"character":"。","confidence":0.9982624650001526}],"itemstring":"除外)。"},{"itemcoord":{"x":233,"y":277,"width":287,"height":18},"words":[{"character":"2","confidence":0.99992036819458},{"character":".","confidence":0.9986629486083984},{"character":"手","confidence":0.9997488856315612},{"character":"感","confidence":0.9999783039093018},{"character":"柔","confidence":0.9998743534088136},{"character":"软","confidence":0.9998101592063904},{"character":"、","confidence":0.9987398982048036},{"character":"毛","confidence":0.9999630451202391},{"character":"圈","confidence":0.9999736547470092},{"character":"蓬","confidence":0.9898577332496644},{"character":"松","confidence":0.9999889135360718},{"character":"、","confidence":0.9989535808563232},{"character":"吸","confidence":0.999909281730652},{"character":"水","confidence":0.999908685684204},{"character":"性","confidence":0.9999761581420898},{"character":"好","confidence":0.999988317489624},{"character":",","confidence":0.9971440434455872},{"character":"接","confidence":0.999951958656311}],"itemstring":"2.手感柔软、毛圈蓬松、吸水性好,接"},{"itemcoord":{"x":248,"y":303,"width":153,"height":17},"words":[{"character":"触","confidence":0.9999226331710817},{"character":"肌","confidence":0.9999467134475708},{"character":"肤","confidence":0.9998550415039064},{"character":"感","confidence":0.9998602867126464},{"character":"觉","confidence":0.9978869557380676},{"character":"自","confidence":0.999998927116394},{"character":"然","confidence":1},{"character":"舒","confidence":0.9999998807907104},{"character":"适","confidence":0.999744951725006},{"character":"。","confidence":0.998598039150238}],"itemstring":"触肌肤感觉自然舒适。"},{"itemcoord":{"x":236,"y":329,"width":265,"height":19},"words":[{"character":"3","confidence":0.999930739402771},{"character":".","confidence":0.9979470372200012},{"character":"染","confidence":0.9999672174453736},{"character":"色","confidence":0.99997079372406},{"character":"全","confidence":0.9999613761901855},{"character":"部","confidence":0.9999263286590576},{"character":"使","confidence":0.999990940093994},{"character":"用","confidence":0.9999885559082032},{"character":"进","confidence":0.9996850490570068},{"character":"口","confidence":0.9999228715896606},{"character":"染","confidence":0.9999171495437622},{"character":"料","confidence":0.9999735355377196},{"character":",","confidence":0.994210958480835},{"character":"色","confidence":0.9999107122421264},{"character":"牢","confidence":0.9999504089355468},{"character":"度","confidence":0.999986171722412},{"character":"好","confidence":0.9999781847000122},{"character":"、","confidence":0.99902081489563}],"itemstring":"3.染色全部使用进口染料,色牢度好、"},{"itemcoord":{"x":247,"y":357,"width":57,"height":16},"words":[{"character":"耐","confidence":0.9995765089988708},{"character":"洗","confidence":0.997870683670044},{"character":"涤","confidence":0.9997075200080872},{"character":"。","confidence":0.999325156211853}],"itemstring":"耐洗涤。"},{"itemcoord":{"x":323,"y":403,"width":98,"height":25},"words":[{"character":"温","confidence":0.9985106587409972},{"character":"馨","confidence":0.9988638162612916},{"character":"提","confidence":0.9998421669006348},{"character":"示","confidence":0.9999939203262328}],"itemstring":"温馨提示"},{"itemcoord":{"x":235,"y":465,"width":280,"height":18},"words":[{"character":"1","confidence":0.9974324107170104},{"character":".","confidence":0.9943867921829224},{"character":"深","confidence":0.9999585151672364},{"character":"色","confidence":0.9999676942825316},{"character":"产","confidence":0.9999828338623048},{"character":"品","confidence":0.9999972581863404},{"character":"初","confidence":0.9999632835388184},{"character":"次","confidence":0.9999954700469972},{"character":"洗","confidence":0.9973767995834352},{"character":"涤","confidence":0.9998528957366944},{"character":"时","confidence":0.999848246574402},{"character":"会","confidence":0.9999499320983888},{"character":"有","confidence":0.9999771118164064},{"character":"轻","confidence":0.999948501586914},{"character":"微","confidence":0.9998568296432496},{"character":"掉","confidence":0.999832272529602},{"character":"色","confidence":0.9998605251312256},{"character":"现","confidence":0.9996801614761353}],"itemstring":"1.深色产品初次洗涤时会有轻微掉色现"},{"itemcoord":{"x":245,"y":492,"width":175,"height":18},"words":[{"character":"象","confidence":0.9999597072601318},{"character":",","confidence":0.9959152340888976},{"character":"割","confidence":0.999987006187439},{"character":"绒","confidence":0.9997709393501282},{"character":"产","confidence":0.9999940395355223},{"character":"品","confidence":0.9546661972999572},{"character":"不","confidence":0.9999688863754272},{"character":"宜","confidence":0.9999117851257324},{"character":"机","confidence":0.9999151229858398},{"character":"洗","confidence":0.9985805749893188},{"character":"。","confidence":0.999201238155365}],"itemstring":"象,割绒产品不宜机洗。"},{"itemcoord":{"x":227,"y":520,"width":289,"height":17},"words":[{"character":"2","confidence":0.9997889399528505},{"character":".","confidence":0.994099497795105},{"character":"产","confidence":0.9999057054519652},{"character":"品","confidence":0.9999979734420776},{"character":"不","confidence":0.999951958656311},{"character":"宜","confidence":0.9996045231819152},{"character":"长","confidence":0.9999979734420776},{"character":"期","confidence":0.9999982118606568},{"character":"处","confidence":0.9999972581863404},{"character":"于","confidence":0.9988164901733398},{"character":"潮","confidence":0.999860405921936},{"character":"湿","confidence":0.9999277591705322},{"character":"状","confidence":0.9999464750289916},{"character":"态","confidence":0.9999442100524902},{"character":",","confidence":0.9985288381576538},{"character":"不","confidence":0.9999818801879884},{"character":"宜","confidence":0.9999547004699708},{"character":"长","confidence":0.9999994039535524}],"itemstring":"2.产品不宜长期处于潮湿状态,不宜长"},{"itemcoord":{"x":244,"y":548,"width":73,"height":18},"words":[{"character":"时","confidence":0.9998537302017212},{"character":"间","confidence":0.9999980926513672},{"character":"曝","confidence":0.9999419450759888},{"character":"晒","confidence":0.9997050166130066},{"character":"。","confidence":0.9992271661758424}],"itemstring":"时间曝晒。"},{"itemcoord":{"x":225,"y":576,"width":212,"height":19},"words":[{"character":"3","confidence":0.999473512172699},{"character":".","confidence":0.9980056881904602},{"character":"不","confidence":0.999929428100586},{"character":"宜","confidence":0.999871015548706},{"character":"与","confidence":0.9999983310699464},{"character":"化","confidence":0.9999493360519408},{"character":"纤","confidence":0.999975085258484},{"character":"织","confidence":0.999985694885254},{"character":"物","confidence":0.9999982118606568},{"character":"同","confidence":0.9999945163726808},{"character":"时","confidence":0.9999511241912842},{"character":"洗","confidence":0.9978109002113342},{"character":"涤","confidence":0.9990622401237488},{"character":"。","confidence":0.9989842772483826}],"itemstring":"3.不宜与化纤织物同时洗涤。"}]
             * session_id : 1255122536976413777
             */
    
            private double angle;
            private String session_id;
            @SerializedName("class")
            private List<?> classX;
            private List<Itemsshiti> items;
    
            public double getAngle() {
                return angle;
            }
    
            public void setAngle(double angle) {
                this.angle = angle;
            }
    
            public String getSession_id() {
                return session_id;
            }
    
            public void setSession_id(String session_id) {
                this.session_id = session_id;
            }
    
            public List<?> getClassX() {
                return classX;
            }
    
            public void setClassX(List<?> classX) {
                this.classX = classX;
            }
    
            public List<Itemsshiti> getItems() {
                return items;
            }
    
            public void setItems(List<Itemsshiti> items) {
                this.items = items;
            }
    
            public static class Itemsshiti {
                /**
                 * itemcoord : {"x":320,"y":171,"width":95,"height":24}
                 * words : [{"character":"产","confidence":0.9999980926513672},{"character":"品","confidence":0.9999918937683105},{"character":"特","confidence":0.9999916553497316},{"character":"点","confidence":0.9999955892562866}]
                 * itemstring : 产品特点
                 */
    
                private Itemcoordshiti itemcoord;
                private String itemstring;
                private List<Wordsshiti> words;
    
                public Itemcoordshiti getItemcoord() {
                    return itemcoord;
                }
    
                public void setItemcoord(Itemcoordshiti itemcoord) {
                    this.itemcoord = itemcoord;
                }
    
                public String getItemstring() {
                    return itemstring;
                }
    
                public void setItemstring(String itemstring) {
                    this.itemstring = itemstring;
                }
    
                public List<Wordsshiti> getWords() {
                    return words;
                }
    
                public void setWords(List<Wordsshiti> words) {
                    this.words = words;
                }
    
                public static class Itemcoordshiti {
                    /**
                     * x : 320
                     * y : 171
                     * width : 95
                     * height : 24
                     */
    
                    private int x;
                    private int y;
                    private int width;
                    private int height;
    
                    public int getX() {
                        return x;
                    }
    
                    public void setX(int x) {
                        this.x = x;
                    }
    
                    public int getY() {
                        return y;
                    }
    
                    public void setY(int y) {
                        this.y = y;
                    }
    
                    public int getWidth() {
                        return width;
                    }
    
                    public void setWidth(int width) {
                        this.width = width;
                    }
    
                    public int getHeight() {
                        return height;
                    }
    
                    public void setHeight(int height) {
                        this.height = height;
                    }
                }
    
                public static class Wordsshiti {
                    /**
                     * character : 产
                     * confidence : 0.9999980926513672
                     */
    
                    private String character;
                    private double confidence;
    
                    public String getCharacter() {
                        return character;
                    }
    
                    public void setCharacter(String character) {
                        this.character = character;
                    }
    
                    public double getConfidence() {
                        return confidence;
                    }
    
                    public void setConfidence(double confidence) {
                        this.confidence = confidence;
                    }
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:腾讯的文字识别ocr的使用

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