我想通過JNI使用FFMpeg的sws_scale函數將Java中的TYPE_3BYTE_BGR BufferedImage轉換爲yuv。我首先提取從我的BufferedImage圖像的數據作爲使用FFMpeg將BGR BufferedImage快速轉換爲YUV
byte[] imgData = ((DataBufferByte) myImage.getRaster().getDataBuffer()).getData();
byte[] output = processImage(toSend,0);
然後我將它傳遞給processImage來函數,它是一個本地函數。 C++的側面看起來是這樣的:
JNIEXPORT jbyteArray JNICALL Java_jni_JniExample_processData
(JNIEnv *env, jobject obj, jbyteArray data, jint index)
{
jboolean isCopy;
uint8_t *test = (uint8_t *)env->GetPrimitiveArrayCritical(data, &isCopy);
uint8_t *inData[1]; // RGB24 have one plane
inData[0] = test;
SwsContext * ctx = sws_getContext(width,height,AV_PIX_FMT_BGR24, (int)width, (int)width,
AV_PIX_FMT_YUV420P, 0, 0, 0, 0);
int lumaPlaneSize = width *height;
uint8_t *yuv[3];
yuv[0] = new uint8_t[lumaPlaneSize];
yuv[1] = new uint8_t[lumaPlaneSize/4];
yuv[2] = new uint8_t[lumaPlaneSize/4];
int inLinesize[1] = { 3*nvEncoder->width }; // RGB stride
int outLinesize[3] = { 3*width ,3*width ,3*width }; // YUV stride
sws_scale(ctx, inData, inLinesize, 0, height , yuv, outLinesize);
但是,在運行代碼後,我得到警告:[swscaler @ 0x7fb598659480] Warning: data is not aligned! This can lead to a speedloss, everything crashes.
,一切都崩潰的最後一行。我是否正確地將正確的參數傳遞給sws_scale? (特別是大步)。
更新: 有一個單獨的錯誤在這裏:SwsContext * ctx = sws_getContext(width,height,AV_PIX_FMT_BGR24, (int)width, (int)width,0,NULL,NULL,NULL)
應改爲:SwsContext * ctx = sws_getContext(width,height,AV_PIX_FMT_BGR24, (int)height, (int)width,0,NULL,NULL,NULL)
感謝您的回覆,但我仍然有一些問題。如果我將步幅調整爲yuv [1] = new uint8_t [lumaPlaneSize/2];和yuv [1] = new uint8_t [lumaPlaneSize/2]; ,你的建議outputLinesize似乎一切正常。除了將輸出寫入文件: fwrite(yuv [0],1,lumaPlaneSize,fp); fwrite(yuv [1],1,lumaPlaneSize/4,fp); fwrite(yuv [2],1,lumaPlaneSize/4,fp); 看來,圖像縮放和不完整。我更改爲fwrite(yuv [1和2],1,lumaPlaneSize/2,fp);那麼圖像就完全不好了我該怎麼辦? – Hossein
什麼不完整的圖像看起來像? – Sergio
之前: https://postimg.org/image/gr93ykns1/ 後: https://postimg.org/image/f0mg9dyvb/ – Hossein