Bitmap too larget to be uploaded into a texture的解決方法


Bitmap too larget to be uploaded into a texture的解決方法

問題描述

使用canvas.drawBitmap()系列方法時,拋出錯誤Bitmap too larget to be uploaded into a texture。

問題原因

錯誤日志的內容是

W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (1204x4533, max=4096x4096)

所以,可以得知問題的原因是設置src的圖片寬高大於了最大接受的值,所以拋出錯誤。
本來想換着想法實現,經過測試發現設置background,src都會有這樣的問題出現。

解決方法

使用BitmapFactory.decodeStream()系列方法將圖片的寬高進行壓縮。

	if (bitmap != null) {
    	int maxWidth = canvas.getMaximumBitmapWidth();
    	int maxHeight =  canvas.getMaximumBitmapHeight();
    	int width = bitmap.getWidth();
    	int height = bitmap.getHeight();
    	if (width > maxWidth || height > maxHeight) {
    		int inSampleSize;
        	int heightRatio = Math.round((float) height / (float) maxHeight);
       		int widthRatio = Math.round((float) width / (float) maxWidth);
        	inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
        	if (inSampleSize == 1) {
        		inSampleSize = 2;
        	}
        	ByteArrayOutputStream baos = new ByteArrayOutputStream();
        	bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        	ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        	BitmapFactory.Options options = new BitmapFactory.Options();
        	options.inSampleSize = inSampleSize;
        	options.inJustDecodeBounds = false;
        	bitmap = BitmapFactory.decodeStream(isBm, null, options);
		}
    }

將上面這段代碼添加到合適的位置,問題就解決了。

參考文章

https://blog.csdn.net/vickywinner/article/details/53164702


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM