2012-03-09 68 views
0

我目前正在嘗試在Android上的圖像上執行相同的操作(OCR),一種方法是使用相機,另一種方法是從SD卡上加載圖像。從相機拍攝時,其中的代碼是引用Android上的SD卡上的文件

Bitmap bitmap = BitmapFactory.decodeFile(_path,options); 

其中_path等於使用該應用程序(_path = DATA_PATH + "ocr.jpg";)最後拍攝的圖像的代碼工作。然而,當我嘗試使用從畫廊,_path就等於選擇了圖像,

imageCaptureUri = data.getData(); 
_path = imageCaptureUri.getPath(); 

程序鎖定了錯誤

失敗deleiving結果ResultInfo {誰= NULL,請求= 2, result = -1,data = intent {dat = content:// media/external/images/media/26 typ = image/jpeg(has extras)}} to activity {com.project.projectActivity}:java.lang.NullPointerException

如果有人知道發生了什麼,我希望聽到你的消息!

回答

2

你可以得到圖像的路徑..

_path = getPath(imageCaptureUri); 


public String getPath(Uri uri) { 
String[] projection = { MediaColumns.DATA }; 
Cursor cursor = managedQuery(uri, projection, null, null, null); 
column_index = cursor 
    .getColumnIndexOrThrow(MediaColumns.DATA); 
cursor.moveToFirst(); 
imagePath = cursor.getString(column_index); 

return cursor.getString(column_index); 
} 

然後

Bitmap bitmap = BitmapFactory.decodeFile(_path,options); 

注意:如果您裁剪圖像,則此方法在此情況下不起作用,Gallery將在裁剪的圖像的相同目錄上生成裁剪圖像。

+0

我現在就試試這個,謝謝! – TomSelleck 2012-03-09 17:33:34

+0

工作就像一個魅力,非常感謝! – TomSelleck 2012-03-09 17:41:14

1

圖庫返回給你一個Uri來訪問圖像。

您需要使用從BitmapFactory的decodeStream方法,爲此,你需要打開給定的URI的InputStream:

InputStream is = getContentResolver().openInputStream(imageCaptureUri); 
Bitmap bitmap = BitmapFactory.decodeStream(is, options); 
+0

現在就試試這個,得到一個說decodeSteam(InputStream)不能應用的錯誤(InputStream,BitmapFactory.Options),也非常感謝您的回覆! – TomSelleck 2012-03-09 17:28:48

+0

是的,我的不好,你需要指定一個出Rect http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeStream%28java.io.InputStream,%20android.graphics.Rect,%20android .graphics.BitmapFactory.Options%29 – njzk2 2012-03-12 09:43:18