其他分享
首页 > 其他分享> > 如何从一个方法返回2个值并在一个类中使用这两个值?

如何从一个方法返回2个值并在一个类中使用这两个值?

作者:互联网

我正在尝试从Microsoft获得此代码,但我想将它们所做的两个功能结合起来.一个是分析图像,一个是检测名人.但是,我在如何从一个函数返回2个值时遇到困难.
这是处理方法……

private String process() throws VisionServiceException, IOException {
    Gson gson = new Gson();
    String model = "celebrities";

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 100, output);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(output.toByteArray());


    AnalysisResult v = this.client.describe(inputStream, 1);
    AnalysisInDomainResult m = this.client.analyzeImageInDomain(inputStream,model);

    String result = gson.toJson(v);
    String result2 = gson.toJson(m);

    Log.d("result", result);

    return result, result2;
}

并将这两种结果与这种方法结合起来……

    @Override
protected void onPostExecute(String data) {
    super.onPostExecute(data);

    mEditText.setText("");
    if (e != null) {
        mEditText.setText("Error: " + e.getMessage());
        this.e = null;
    } else {
        Gson gson = new Gson();
        AnalysisResult result = gson.fromJson(data, AnalysisResult.class);
        //pang detect ng peymus...
        AnalysisInDomainResult result2 = gson.fromJson(data, AnalysisInDomainResult.class);

        //decode the returned result
        JsonArray detectedCelebs = result2.result.get("celebrities").getAsJsonArray();
        if(result2.result != null){
            mEditText.append("Celebrities detected: "+detectedCelebs.size()+"\n");
            for(JsonElement celebElement: detectedCelebs) {
                JsonObject celeb = celebElement.getAsJsonObject();
                mEditText.append("Name: "+celeb.get("name").getAsString() +", score" +
                        celeb.get("confidence").getAsString() +"\n");
            }
        }else {
            for (Caption caption: result.description.captions) {
                mEditText.append("Your seeing " + caption.text + ", confidence: " + caption.confidence + "\n");
            }
            mEditText.append("\n");
        }


       /* for (String tag: result.description.tags) {
            mEditText.append("Tag: " + tag + "\n");
        }
        mEditText.append("\n");
        mEditText.append("\n--- Raw Data ---\n\n");
        mEditText.append(data);*/
        mEditText.setSelection(0);
    }
}

提前致谢!

解决方法:

您可以使用.参数是两个对象,所以你可以放一切

  final Pair<String, String> pair = Pair.create("1", "2");
  String a = pair.first;
  String b = pair.second;

标签:java,android,return,microsoft-cognitive
来源: https://codeday.me/bug/20190717/1486445.html