ImageConverter引起的 invalid address or address of corrupt block 0xb7feab58 passed to dlfree


虹軟人臉識別,其方法要傳NV21格式的byte[], github上有一個虹軟的Demo,是不是虹軟工作人員寫的不清楚,這個Demo里bitmap轉NV21格式byte[]用的是一個第三方庫https://github.com/gqjjqg/android-extend,
用法如下:

ImageConverter convert = new ImageConverter();
convert.initial(mBitmap.getWidth(), mBitmap.getHeight(), ImageConverter.CP_PAF_NV21);
if (convert.convert(mBitmap, data)) {
Log.d(TAG, "convert ok!");
}
convert.destroy();
!

  


本來是沒有問題,但是我這邊需求是大量檢測照片,所以會頻繁多次調用這個方法,以900次為例,在某些平板上是沒有問題的,但是個別平板在100次左右時會報錯:

07-18 21:44:53.719 6365-6609/cn.gxh.face A/libc: invalid address or address of corrupt block 0xb80402f8 passed to dlfree
Fatal signal 11 (SIGSEGV), code 1, fault addr 0xdeadbaad in tid 6609 (pool-2-thread-2)
07-18 21:44:53.820 175-175/? E/DEBUG: Failed to find a valid tombstone, default to using tombstone 0.
failed to open tombstone file '/data/tombstones/tombstone_00': No such file or directory

  

解決方法:可以換一個轉換方法。

public byte[] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {

int[] argb = new int[inputWidth * inputHeight];

scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2];

encodeYUV420SP(yuv, argb, inputWidth, inputHeight);

return yuv;
}

private void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
int frameSize = width * height;

int yIndex = 0;
int uvIndex = frameSize;

int R, G, B, Y, U, V;
int index = 0;

for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff);

// well known RGB to YUV algorithm
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;

yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));

if (j % 2 == 0 && index % 2 == 0 && uvIndex < yuv420sp.length - 2) {
yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
}
index++;
}
}
}

  

經測試,可用。


免責聲明!

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



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