我得到了一个类,它需要变量Context& URI
如何从其他类
调用此函数我把MainActivity.this,uri
但仍然有一个NULL字符串
为什么方法“发送”不会发生?
Main.class
String predict = "Default";
predict = VisionClient.send(MainActivity.this, uri);
Log.e("try","predict =>>>>>>> " + predict); // predict got "Default"
VisionClient.class
public final static String send(Context context, Uri uriImage) throws Exception{
InputStream credentialsStream = context.getResources().openRawResource(R.raw.calories_4348f6cf420);
GoogleCredential credential = null;
credential = GoogleCredential.fromStream(credentialsStream, AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory()).createScoped(VisionScopes.all());
Vision vision = new Vision(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), credential);
String path = ImagenMediaStoreUtils.getPath(context, uriImage);
File wiki_path = new File(path);
Image image = new Image().encodeContent(readFileToBytes(wiki_path));
BatchAnnotateImagesRequest content = new BatchAnnotateImagesRequest();
Feature textDetection = new Feature();
textDetection.setType("TEXT_DETECTION");
textDetection.setMaxResults(1);
List<Feature> featureList = Lists.newArrayList();
featureList.add(textDetection);
List<AnnotateImageRequest> anImgReqList = Lists.newArrayList();
AnnotateImageRequest annotatedImageReq = new AnnotateImageRequest();
annotatedImageReq.setFeatures(featureList);
annotatedImageReq.setImage(image);
anImgReqList.add(annotatedImageReq);
content.setRequests(anImgReqList);
BatchAnnotateImagesResponse response = vision.images().annotate(content).execute();
if(response.getResponses() == null || response.getResponses().get(0) == null
|| response.getResponses().get(0).getTextAnnotations() == null
|| response.getResponses().get(0).getTextAnnotations().get(0) == null
|| response.getResponses().get(0).getTextAnnotations().get(0).getDescription() == null){
throw new Exception("response null");
}
return response.getResponses().get(0).getTextAnnotations().get(0).getDescription();
}
答案 0 :(得分:1)
确保你做两件事:
try {
Thread t1 = new Thread(new Runnable(){
public void run(){
String response = null;
try {
response = VisionClient.send(context, photoFile, mCurrentPhotoPath);
} catch (Exception e) {
e.printStackTrace();
}
txtComputerVisionTextResult.setText(response);
Toast toast = Toast.makeText(context, "response = " + response, Toast.LENGTH_LONG);
toast.show();
}
}
);
t1.start();
和
<asp:ListView ID="LVCateMenu" runat="server">
<ItemTemplate>
<li class="<%=Response.Write(ActiveClass);%>"><asp:HyperLink runat="server" NavigateUrl="#"><%#Eval("cate_name") %></asp:HyperLink></li>
</ItemTemplate>
</asp:ListView>