多亏了这个libjpeg-turbo for android,我终于成功构建了libjpeg-turbo静态库,现在我有了一个libjpeg.a和一个由ndk-build生成的libsimd.a
但是我还没有找到任何关于下一步该怎么做的信息?我正在使用BitmapFactory中的build将缓冲区(从套接字)中的jpeg解码为位图,它工作得很好
byte[] jpgBits = new byte[jpgBitsLen];
dis.readFully(jpgBits);
Bitmap bmp = BitmapFactory.decodeByteArray(jpgBits, 0, jpgBitsLen);如何用libjpeg-turbo替换BitmapFactory.decodeByteArray?
我用这个在我的PC上编码了这个流
tjhandle rmfdJpegCompressor = tjInitCompress();
tjCompress2(rmfdJpegCompressor, (unsigned char *)s_rmfdPixels, MFD_WH, 0, MFD_WH, TJPF_BGRX,
&rmfdBits, &rmfdBitsLen, TJSAMP_420, RMFD_JPEG_QUALITY,
0);
tjDestroy(rmfdJpegCompressor);它工作得很好,所以我想肯定有android的等价物?
我读到了这个https://wiki.linaro.org/BenjaminGaignard/libjpeg-turboAndSkia,这是不是意味着使用它的唯一方法就是重新构建android源代码,使其使用libjpeg-turbo?我在某处读到有一个兼容性api和一个用于libjpeg-turbo的本机api,我很高兴使用任何最简单的api,因为我不想重建android
我尝试了以下方法:在项目根目录下创建文件夹jni/include并将turbojpeg.h放入项目根目录下创建文件夹jni/prebuilt并将libjpeg.a放入其中
在我的java代码中,我将
private native int tjInitDecompress(); 在MainActivity和onCreate中我添加了
int i = tjInitDecompress();
Log.d("MainActivity", "i="+i);它构建并运行,但在tjInitDecompress上崩溃
在日志中它说:没有找到本机Lcom/example.jpegtest/MainActivity;.tjInitDecompress ()的实现
谢谢
发布于 2014-07-04 17:37:52
好吧,这是一大堆的工作,但我最终得到了一些工作,所以我想让任何感兴趣的人知道我是如何做到的。
首先,我构建了hello-jin演示,如下所述:https://developer.android.com/tools/sdk/ndk/index.html
然后,我创建了一个新项目,复制了jni并更改了c函数的名称,以匹配新的包和类名。不要在包名中使用-和_,否则会有问题。只要a-x0-9就是最好的。
然后,我将所有libjpeg-turbo文件和目录复制到jni中,并测试ndk-build仍然可以工作
然后,我为libjpg funs创建了一个jni包装器,比如tjpegini-arm.c
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <jni.h>
#include "turbojpeg.h"
/*
* Class: libjpegturbo_jniapi
* Method: tjInitDecompress
* Signature: ()I
*/
//package com.design2112.fbmslpit
//public class MainActivity
jint JNICALL Java_com_design2112_fbmslpit_MainActivity_tjInitDecompress
(JNIEnv *env, jobject thisObj)
{
return (int)tjInitDecompress();
}
/*
* Class: libjpegturbo_jniapi
* Method: tjDecompressHeader2
* Signature: (I[BI)I
*/
jint JNICALL Java_com_design2112_fbmslpit_MainActivity_tjDecompressHeader2
(JNIEnv *env, jobject thisObj, jint handle, jbyteArray jpegBuf, jint jpegSize)
{
jbyte *real_jpegBuf = (*env)->GetByteArrayElements(env, jpegBuf, 0);
if (!real_jpegBuf) return -1;
//jsize length = (*env)->GetArrayLength(env, real_jpegBuf);
/*for (i = 0; i < length; i++) {
sum += inCArray[i];
}*/
int width, height, jpegSubsamp;
int ret = tjDecompressHeader2((tjhandle)handle,
(unsigned char *)real_jpegBuf, (unsigned long)jpegSize, &width, &height,
&jpegSubsamp);
if(ret!=0) {
return 0;
}
// ok, so pack width and height together
return width<<16 | height;
}
/*
* Class: libjpegturbo_jniapi
* Method: tjDecompress2
* Signature: (I[BI[IIIIII)V
*/
void JNICALL Java_com_design2112_fbmslpit_MainActivity_tjDecompress2
(JNIEnv *env, jobject thisObj, jint handle, jbyteArray jpegBuf, jint jpegSize, jintArray dstBuf,
jint width, jint pitch, jint height, jint pixelFormat, jint flags)
{
jbyte *real_jpegBuf = (*env)->GetByteArrayElements(env, jpegBuf, 0);
if (!real_jpegBuf) return;
jint *real_dstBuf = (*env)->GetIntArrayElements(env, dstBuf, 0);
if (!real_dstBuf) return;
jsize length = (*env)->GetArrayLength(env, jpegBuf);
tjDecompress2((tjhandle)handle,
(unsigned char *)real_jpegBuf, (unsigned long)jpegSize, (unsigned char *)real_dstBuf,
width, pitch, height, pixelFormat, flags);
}
/*
* Class: libjpegturbo_jniapi
* Method: tjDestroy
* Signature: (I)V
*/
void JNICALL Java_com_design2112_fbmslpit_MainActivity_tjDestroy
(JNIEnv *env, jobject thisObj, jint handle)
{
tjDestroy((tjhandle)handle);
}重要的是,您必须将com_design2112_fbmslpit_MainActivity重命名为您的包和类,这样才能正常工作
将tjpegini-arm.c添加到Android.mk生成文件,然后在jni目录中运行ndk-build
ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk obj/local/armeabi/libjpeg.so LOCAL_ARM_MODE=arm并将.so复制到正确的名称和位置
cp obj/local/armeabi/libjpeg.so ../libs/armeabi/libtjpegjni-arm.so然后在我的MainAvtivity.java中
public class MainActivity extends Activity {
public native int tjInitDecompress();
public native int tjDecompressHeader2(int handle, byte[] jpegBits, int jpegBitsLen);
public native void tjDecompress2(int handle, byte[] jpegBits,
int jpegBitsLen, int[] outbuffer, int width, int pitch, int height,
int pixelFormat, int flags);
public native void tjDestroy(int handle);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"/Download/test.jpg");
int jpegBitsLen = (int) file.length();
byte[] jpegBits = new byte[jpegBitsLen];
DataInputStream dis;
try {
dis = new DataInputStream(new FileInputStream(file));
dis.readFully(jpegBits);
dis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.loadLibrary("tjpegjni-arm");
int jpegDec = tjInitDecompress();
int wh = tjDecompressHeader2(jpegDec, jpegBits, jpegBitsLen);
int width = wh>>16;
int height = wh&0x7fff;
int[] buffer = new int[width*height];
tjDecompress2(jpegDec, jpegBits, jpegBitsLen, buffer, width, 0/*pitch*/, height, 2 /*TJPF_RGBX*/, 0);
tjDestroy(jpegDec);
Bitmap bmp = Bitmap.createBitmap(buffer, width, height, Bitmap.Config.ARGB_8888);
}基本上就是这样。您可以以任何想要的方式显示bmp。
这也是我一堆狗屎的工作,想弄清楚根本没有jni ndk经验。如果有人觉得这很有用,给我发邮件喝杯啤酒。
更新,这是一个令人震惊的消息,解码一张450x450的图像需要20ms。内置的BitmapFactory.decodeByteArray几乎可以做到这一点!
如果其他人尝试了这个方法并得到了不同的结果,请记录下来
https://stackoverflow.com/questions/24548952
复制相似问题