请任何人帮我转换C中的这个Android代码。我想在C NDK中做这个部分。这是我到目前为止所尝试的。我无法转换android的getPixel()
和setPixel()
方法。请参阅下面的代码。
JNIEXPORT jobject JNICALL
Java_com_test_mypic_StylePreviewActivity_mergeBitmaps(JNIEnv *env, jobject instance,
jobject bm, jobject filter) {
AndroidBitmapInfo bm1;
AndroidBitmap_getInfo(env, bm, &bm1);
AndroidBitmapInfo filter1;
AndroidBitmap_getInfo(env, filter, &filter1);
int width = bm1.width;
int height = bm1.height;
int w2 = filter1.width;
int h2 = filter1.height;
float scaleX = (float) w2 / (float) width;
Bitmap result = Bitmap.createBitmap(w2, h2, ANDROID_BITMAP_FORMAT_RGB_565);
void* bitmapPixels;
AndroidBitmap_lockPixels(env, bm, &bitmapPixels);
for (int x = 0; x < w2; x++) {
for (int y = 0; y < h2 && y < height; y++) {
int xx =(int) ((float) x / scaleX);
int yy = (int) ((float) y / scaleX);
int pixel = bm1.getPixel(xx, yy);
int fp = filter1.getPixel(x, y);
int alpha = (fp & 0xFF000000) >> 24;
if (alpha == 0) {
result.setPixel(x, y, pixel);
}
}
}
result = Bitmap.createBitmap(result, 0, 0, width, height);
return result;
}
在@Serhio的帮助下,我写了这个方法。但它似乎有所作为 在这里错了。请有人检查一下。
JNIEXPORT jobject JNICALL
Java_com_test_mypic_StylePreviewActivity_mergeBitmaps(JNIEnv *env, jobject instance,
jobject bm, jobject filter, jobject result) {
AndroidBitmapInfo bm1;
AndroidBitmap_getInfo(env, bm, &bm1);
AndroidBitmapInfo filter1;
AndroidBitmap_getInfo(env, filter, &filter1);
// AndroidBitmapInfo result1;
// AndroidBitmap_getInfo(env, result, &result1);
int width = bm1.width;
int height = bm1.height;
int w2 = filter1.width;
int h2 = filter1.height;
float scaleX = (float) w2 / (float) width;
for (int x = 0; x < w2; x++) {
for (int y = 0; y < h2 && y < height; y++) {
int xx =(int) ((float) x / scaleX);
int yy = (int) ((float) y / scaleX);
int pixel = getPixel(env,bm, xx, yy);
int fp = getPixel(env,filter,x, y);
int alpha = (fp & 0xFF000000) >> 24;
if (alpha == 0) {
setPixel(env, result, x, y, pixel);
}
}
}
return result;
}
答案 0 :(得分:1)
本机代码中没有$('[id*="DTE_field_"]').attr('id', function(i, oldId) {
return oldId.replace('DTE_field_', '');
});
的直接模拟。取而代之的是,通过getPixel()/setPixel()
获取指向像素缓冲区的原始指针,然后您可以自由修改此缓冲区中的数据。要获取/设置单个像素,您可以使用下一个功能:
AndroidBitmap_lockPixels()
当然,您只需锁定一次缓冲区并循环遍历像素。
尽管如此,您的代码似乎应该执行应用于位图的掩码。可能只有通过使用适当的#include <stdint.h>
#include <assert.h>
#include <jni.h>
#include <android/bitmap.h>
uint32_t getPixel(JNIEnv *env, jobject bm, int x, int y)
{
AndroidBitmapInfo bi = {0};
uint8_t *pixelBuf;
uint8_t a, r, g, b;
AndroidBitmap_getInfo(env, bm, &bi);
/* ensure that we fit into bounds */
assert(x >= 0 && x < bi.width
&& y >= 0 && y < bi.height);
/* we support only one format at the moment */
assert(ANDROID_BITMAP_FORMAT_RGBA_8888 == bi.format);
/* read pixel components */
AndroidBitmap_lockPixels(env, bm, (void **)&pixelBuf);
r = pixelBuf[y * bi.stride + x * 4 + 0];
g = pixelBuf[y * bi.stride + x * 4 + 1];
b = pixelBuf[y * bi.stride + x * 4 + 2];
a = pixelBuf[y * bi.stride + x * 4 + 3];
AndroidBitmap_unlockPixels(env, bm);
return a << 24 | r << 16 | g << 8 | b;
}
void setPixel(JNIEnv *env, jobject bm, int x, int y, uint32_t val)
{
AndroidBitmapInfo bi = {0};
uint8_t *pixelBuf;
AndroidBitmap_getInfo(env, bm, &bi);
/* ensure that we fit into bounds */
assert(x >= 0 && x < bi.width
&& y >= 0 && y < bi.height);
/* we support only one format at the moment */
assert(ANDROID_BITMAP_FORMAT_RGBA_8888 == bi.format);
/* read pixel components */
AndroidBitmap_lockPixels(env, bm, (void **)&pixelBuf);
pixelBuf[y * bi.stride + x * 4 + 0] = (val >> 16) & 0xff;
pixelBuf[y * bi.stride + x * 4 + 1] = (val >> 8) & 0xff;
pixelBuf[y * bi.stride + x * 4 + 2] = (val >> 0) & 0xff;
pixelBuf[y * bi.stride + x * 4 + 3] = (val >> 24) & 0xff;
AndroidBitmap_unlockPixels(env, bm);
}
在位图驱动的画布上绘制,才能在java代码中完成。 See here。它将在本机代码中完成,因此性能会很好。