4

我已經嘗試了很多方法,但我無法成功。我還沒有發現任何源代碼示例爲Android(約rekognition)如何使用Android Studio在亞馬遜重新識別AWS中識別出自動識別區域?

有一個在JAVA源代碼的開發者指南中,但我不能執行,即使我試過TT

我試圖通過發送到人臉從外部存儲器中(模擬器) 圖像文件,我不知道我做錯了什麼(我不是在編碼好) 這裏是我的代碼

AmazonRekognitionClient amazonRekognitionClient; 
Image getAmazonRekognitionImage; 
DetectFacesRequest detectFaceRequest; 
DetectFacesResult detectFaceResult; 
File file = new File(Environment.getExternalStorageDirectory(),"sungyeol.jpg.jpg"); 

public void test_00(View view) { 
ByteBuffer imageBytes; 
try{ 
     InputStream inputStream = new FileInputStream(file.getAbsolutePath().toString()); 
     imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream)); 
     Log.e("InputStream: ",""+inputStream); 
     Log.e("imageBytes: ",""); 
     getAmazonRekognitionImage.withBytes(imageBytes); 

     // Initialize the Amazon Cognito credentials provider 
     CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
       getApplicationContext(), 
       "us-east-2:.......", // Identity Pool ID 
       Regions.US_EAST_2 // Region 
     ); 

     //I want "ALL" attributes 
     amazonRekognitionClient = new AmazonRekognitionClient(credentialsProvider); 

     detectFaceRequest = new DetectFacesRequest() 
       .withAttributes(Attribute.ALL.toString()) 
       .withImage(getAmazonRekognitionImage); 

     detectFaceResult = amazonRekognitionClient.detectFaces(detectFaceRequest); 
     detectFaceResult.getFaceDetails(); 

    } 
catch(Exception ex){ 
    Log.e("Error on something:","Message:"+ex.getMessage()); 
} 

,這裏是我的錯誤

02-04 09:30:07.268 29405-29405/? E/InputStream:: [email protected] 
02-04 09:30:07.271 29405-29405/? E/Error on something:: Message:Attempt to invoke virtual method 'com.amazonaws.services.rekognition.model.Image com.amazonaws.services.rekognition.model.Image.withBytes(java.nio.ByteBuffer)' on a null object reference 

什麼是空對象引用? 我試圖改變文件路徑,但他說沒有這樣的文件...當我改變到這個路徑,上面有錯誤。 順便說一句,我已經問過用戶的權限來訪問Android中的模擬器的文件夾

請幫我 PS。對不起我的英文不好

謝謝你提前。

回答

2

現在我確定有問題 我已經通過馬努很多事情 謝謝

我是泰國,我不得不更努力地嘗試尋找解決方案,因爲那裏缺乏以特定語言提供的信息,但這裏是我的解決方案。

我的解決方案是:

0.There是設置爲Rekognition端點 - > http://docs.aws.amazon.com/general/latest/gr/rande.html#rekognition_region

1.On一個「空對象引用的問題:」我發現,我要創建一個首先創建新對象,如「圖像圖像=新圖像()」; < - 「新」命令,在該類

2.After上述錯誤創建一個對象實例,有更多的錯誤(在NetworkOnMainThreadException錯誤),所以我嘗試了一切,直到我發現這個頁面 - > https://docs.aws.amazon.com/cognito/latest/developerguide/getting-credentials.html網頁說,...

enter image description here

因此,我擡頭有關的AsyncTask的詳細信息之後,我創建了一個的AsyncTask類,然後將我的所有代碼有關初始化的請求,對AsyncTask類的響應。 ตอนรันตอนท้ายๆน้ำตาจิไหลmy code worked ... TT and by the the the the sungyeol.jpg。JPG文件工作

例如

private void testTask(){ 
    .... all code in the main thread particularly on the requests and responses 
    from the services 
//print the response or the result 
//Log.e() makes the message in the android monitor red like an error 
Log.e("Response:", [responseparameter.toString()]); 

} 

//create the inherited class from the AsyncTask Class 
//(you can create within your activity class) 
class AsyncTaskRunner extends AsyncTask<String,String,String>{ 
    @Override 
    public String doInBackground(String ... input){ 
     testTask(); // call the testTask() method that i have created 
     return null; // this override method must return String 
    } 

} 

//I've created a button for running the task 
public void buttonTask(View view){ 
    AsyncTaskRunner runner = new AsyncTaskRunner(); 
    runner.execute(); 

} 

有關的AsyncTask的更多信息:

https://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask

http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html#.WJdkqVOLTIU

我希望這些幫助:)

+0

這將是有益的如果你可以發佈你的解決方案,我是在相同的情況下。 –

+0

我編輯了我的帖子以獲取更多信息,我希望這些可以幫助您。 –