我正在尝试将 jpeglib-turbo 与 android ndk 一起使用来获取 jpeg 图像的像素 rgb 值,而且我对 C++、C 和 android NDK 都是新手,直到现在我已经尝试使用示例或教程中提供的解决方案使用 libjpeg-turbo 的 TurboJPEG但我无法解决手头的问题,目前使用https://github.com/openstf/android-libjpeg-turbo构建 libjpeg-turbo。
当前代码是
#include <turbojpeg.h>
#include <jni.h>
#include <android/log.h>
#include <syslog.h>
JNIEXPORT jbyteArray
Java_com_serelay_jpegturbo_MainActivity_getImagePixelData( JNIEnv* env,
jobject this,
jbyteArray data)
{
long unsigned int _jpegSize=2464742; //!< _jpegSize from above
unsigned char *_compressedImage; //!< _compressedImage from above
_compressedImage = data;
int width, height, jpegSubSamp;
// int jpegSubSamp = TJSAMP_444;
tjhandle _jpegDecompressor = tjInitDecompress();
tjDecompressHeader2(_jpegDecompressor, _compressedImage, _jpegSize, &width,
&height, &jpegSubSamp);
unsigned long len = width * height * 4;
unsigned long pitch = width * 4;
syslog(LOG_CRIT, "====================");
syslog(LOG_CRIT, "jpegSize %lu", _jpegSize);
syslog(LOG_CRIT, "width %d", width);
syslog(LOG_CRIT, "height %d", height);
syslog(LOG_CRIT, "total length %lu", len);
syslog(LOG_CRIT, "====================");
unsigned char buffer[len]; //!< will contain the decompressed image
tjDecompress2(_jpegDecompressor, _compressedImage, _jpegSize, buffer, width, pitch, height, TJPF_RGBA, 0);
tjDestroy(_jpegDecompressor);
// char array to byte array
jbyteArray jbytes = buffer;
//jbyteArray jbytes = _compressedImage;
return jbytes;
}
要从 java 调用它,我使用公共类 MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
System.loadLibrary("twolib-second");
InputStream inputStream= getResources().openRawResource(R.raw.image1);
byte[] bytes;
try {
bytes = new byte[inputStream.available()];
inputStream.read(bytes);
byte[] array = getImagePixelData(bytes);
tv.setText(String.valueOf(getImagePixelData(bytes).length));
} catch (IOException e) {
e.printStackTrace();
}
// tv.setText(getImagePixelData(5));
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native byte[] getImagePixelData(byte[] x);
}
在ndk生成的so文件的帮助下,我已经成功地从java代码调用,但我面临的问题是代码中的异常,即
信号11(SIGSEGV),代码1(SEGV_MAPERR),故障地址0xeaa6cc30
在进一步检查中,我记录了一些变量的值,考虑到正在传递的图像大小为 3840 * 2160,这些变量感觉有点糟糕,在记录我得到的变量时
03-07 20:26:05.180 13613-13613/com.serelay.jpegturbo E/com.serelay.jpegturbo:
====================
03-07 20:26:05.180 13613-13613/com.serelay.jpegturbo E/com.serelay.jpegturbo:
jpegSize 2464742
03-07 20:26:05.180 13613-13613/com.serelay.jpegturbo E/com.serelay.jpegturbo:
width 4
03-07 20:26:05.180 13613-13613/com.serelay.jpegturbo E/com.serelay.jpegturbo:
height -1386946560
03-07 20:26:05.180 13613-13613/com.serelay.jpegturbo E/com.serelay.jpegturbo:
total length 3578658816
03-07 20:26:05.180 13613-13613/com.serelay.jpegturbo E/com.serelay.jpegturbo:
====================
我可以看到宽度和高度值非常错误,但我不知道为什么以及如何解决它。如果可以提供任何帮助或指导来解决此问题,那就太好了,自从过去 2 天以来,我一直坚持这一点 :) 谢谢。