回答
它實際上是非常簡單的事情。使用
Bitmap yourBitmap = Bitmap.createBitmap(sourceBitmap, x to start from, y to start from, width, height)
更新:使用BitmapRegionDecoder
試試這個
InputStream istream = null;
try {
istream = this.getContentResolver().openInputStream(yourBitmapUri);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(istream, false);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bMap = decoder.decodeRegion(new Rect(istream, x to start from, y to start from, x to end with, y to end with), null);
imageView.setImageBitmap(bMap);
oops ..sorry ..此外,還有一件事是爲sdk版本2.3或更高版本... – Renjith 2012-07-27 10:59:23
@RKN
你的方法也可以拋出的OutOfMemoryError異常 - 如果裁剪位圖超過VM。
我的方法將你和防範這種exeption: (L,T,R,B - 圖像%)
Bitmap cropBitmap(ContentResolver cr, String file, float l, float t, float r, float b)
{
try
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.decodeFile(file, options);
int oWidth = options.outWidth;
int oHeight = options.outHeight;
InputStream istream = cr.openInputStream(Uri.fromFile(new File(file)));
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(istream, false);
if (decoder != null)
{
options = new BitmapFactory.Options();
int startingSize = 1;
if ((r - l) * oWidth * (b - t) * oHeight > 2073600)
startingSize = (int) ((r - l) * oWidth * (b - t) * oHeight/2073600) + 1;
for (options.inSampleSize = startingSize; options.inSampleSize <= 32; options.inSampleSize++)
{
try
{
return decoder.decodeRegion(new Rect((int) (l * oWidth), (int) (t * oHeight), (int) (r * oWidth), (int) (b * oHeight)), options);
}
catch (OutOfMemoryError e)
{
Continue with for loop if OutOfMemoryError occurs
}
}
}
else
return null;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
,並返回最大可用的位圖或空
如果'startingSize'> 32最初是因爲輸入圖像太大? – TWiStErRob 2014-07-25 10:15:52
使用RapidDecoder。
,簡單地做到這一點
import rapid.decoder.BitmapDecoder;
Rect bounds = new Rect(left, top, right, bottom);
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image)
.region(bounds).decode();
它需要Android 2.2或以上。
您可以使用以下算法
- 計算最大可能inSampleSize仍然產生一個 圖像比你的目標較大的出完全加載位圖加載位圖的縮放版本。
- 使用 BitmapFactory.decodeFile(文件,選項)加載圖像,作爲 選項傳入inSampleSize。
- 使用 Bitmap.createScaledBitmap()調整到所需的尺寸。
查看以下帖子 Android: Resize a large bitmap file to scaled output file瞭解更多詳情。
- 1. 如何在D3DXLoadSurfaceFromFile中只使用一部分加載的位圖?
- 2. 只加載pygame中的圖像的一部分?
- 3. 只有部分CSS文件在Chrome中加載
- 4. Android谷歌地圖api只顯示kml文件的一部分
- 5. 如何確保我只在我的部分中加載JS文件一次?
- 6. C# - 加載圖像只是部分
- 7. 只讀文件中的一個部分
- 8. 只在PHP中加載外部PHP文件一次
- 9. HTML文件加載部分
- 10. 加載所需分辨率圖像的一部分android
- 11. 加載位圖文件(.bmp)
- 12. Android WebView加載網站的一部分
- 13. 在Android中繪製位圖部分?
- 14. 如何在Android中裁剪圖像文件的一部分?
- 15. Android加載的大位圖
- 16. 在Android WebView中只加載一個div
- 17. 在Android中正確加載位圖
- 18. 避免重新加載部分視圖中的JavaScript文件
- 19. 爲什麼Meteor只在部署後加載一個CSS文件?
- 20. 只上傳文件的一部分
- 21. 在Tooltip中加載部分視圖
- 22. 如何在android webview中加載網頁的一部分?
- 23. 在ImageView上加載位圖圖像android
- 24. 部分加載的JavaScript文件
- 25. 在JavaScript文件中有條件地加載js的部分Wordpress
- 26. 如何將JSON文件的一部分加載到DataFrame中?
- 27. 在android視圖中重繪只是一部分
- 28. 一次只加載一個Ember應用程序的一部分
- 29. 從外部位置加載JSP文件
- 30. 查找文件的位置只給出了一部分靈活的文件名
感謝史蒂夫,但這需要我首先加載sourceBitmap,我試圖避免這樣做,因爲它太大而不適合內存,我想使用未經過下采樣的位圖。 – Schermvlieger 2010-04-09 10:49:48
糟糕,你是對的,我沒有正確地閱讀你的問題的一部分。我不確定如何在沒有縮減採樣的情況下做到這一點。事實上,一個非常類似的問題也出現在我自己的數字化工作列表中! – 2010-04-09 11:10:23