- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我决定,由于位图占用大量内存,很容易导致内存不足错误,因此我将把艰巨的内存消耗工作放在 C/C++ 代码上。
尽管一切似乎都运行无误,但输出图像并不是原始图像的旋转。事实上,它完全毁了它。
旋转应逆时针旋转 90 度。
因此,正如您所看到的,不仅颜色变得更奇怪,而且尺寸也与我设置的不匹配。这里确实有些奇怪。
也许我没有正确读取/输入数据?
当然这只是一个例子。只要设备有足够的内存来容纳代码,该代码就可以在任何位图上正常工作。此外,除了旋转位图之外,我可能还想对位图执行其他操作。
Android.mk文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := JniTest
LOCAL_SRC_FILES := JniTest.cpp
LOCAL_LDLIBS := -llog
LOCAL_LDFLAGS += -ljnigraphics
include $(BUILD_SHARED_LIBRARY)
APP_OPTIM := debug
LOCAL_CFLAGS := -g
cpp文件:
#include <jni.h>
#include <jni.h>
#include <android/log.h>
#include <stdio.h>
#include <android/bitmap.h>
#include <cstring>
#include <unistd.h>
#define LOG_TAG "DEBUG"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
extern "C"
{
JNIEXPORT jobject JNICALL Java_com_example_jnitest_MainActivity_rotateBitmapCcw90(JNIEnv * env, jobject obj, jobject bitmap);
}
JNIEXPORT jobject JNICALL Java_com_example_jnitest_MainActivity_rotateBitmapCcw90(JNIEnv * env, jobject obj, jobject bitmap)
{
//
//getting bitmap info:
//
LOGD("reading bitmap info...");
AndroidBitmapInfo info;
int ret;
if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0)
{
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return NULL;
}
LOGD("width:%d height:%d stride:%d", info.width, info.height, info.stride);
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
{
LOGE("Bitmap format is not RGBA_8888!");
return NULL;
}
//
//read pixels of bitmap into native memory :
//
LOGD("reading bitmap pixels...");
void* bitmapPixels;
if ((ret = AndroidBitmap_lockPixels(env, bitmap, &bitmapPixels)) < 0)
{
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
return NULL;
}
uint32_t* src = (uint32_t*) bitmapPixels;
uint32_t* tempPixels = new uint32_t[info.height * info.width];
int stride = info.stride;
int pixelsCount = info.height * info.width;
memcpy(tempPixels, src, sizeof(uint32_t) * pixelsCount);
AndroidBitmap_unlockPixels(env, bitmap);
//
//recycle bitmap - using bitmap.recycle()
//
LOGD("recycling bitmap...");
jclass bitmapCls = env->GetObjectClass(bitmap);
jmethodID recycleFunction = env->GetMethodID(bitmapCls, "recycle", "()V");
if (recycleFunction == 0)
{
LOGE("error recycling!");
return NULL;
}
env->CallVoidMethod(bitmap, recycleFunction);
//
//creating a new bitmap to put the pixels into it - using Bitmap Bitmap.createBitmap (int width, int height, Bitmap.Config config) :
//
LOGD("creating new bitmap...");
jmethodID createBitmapFunction = env->GetStaticMethodID(bitmapCls, "createBitmap", "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
jstring configName = env->NewStringUTF("ARGB_8888");
jclass bitmapConfigClass = env->FindClass("android/graphics/Bitmap$Config");
jmethodID valueOfBitmapConfigFunction = env->GetStaticMethodID(bitmapConfigClass, "valueOf", "(Ljava/lang/String;)Landroid/graphics/Bitmap$Config;");
jobject bitmapConfig = env->CallStaticObjectMethod(bitmapConfigClass, valueOfBitmapConfigFunction, configName);
jobject newBitmap = env->CallStaticObjectMethod(bitmapCls, createBitmapFunction, info.height, info.width, bitmapConfig);
//
// putting the pixels into the new bitmap:
//
if ((ret = AndroidBitmap_lockPixels(env, newBitmap, &bitmapPixels)) < 0)
{
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
return NULL;
}
uint32_t* newBitmapPixels = (uint32_t*) bitmapPixels;
int whereToPut = 0;
for (int x = info.width - 1; x >= 0; --x)
for (int y = 0; y < info.height; ++y)
{
uint32_t pixel = tempPixels[info.width * y + x];
newBitmapPixels[whereToPut++] = pixel;
}
AndroidBitmap_unlockPixels(env, newBitmap);
//
// freeing the native memory used to store the pixels
//
delete[] tempPixels;
return newBitmap;
}
java文件:
static
{
System.loadLibrary("JniTest");
}
/**
* rotates a bitmap by 90 degrees counter-clockwise . <br/>
* notes:<br/>
* -the input bitmap will be recycled and shouldn't be used anymore <br/>
* -returns the rotated bitmap . <br/>
* -could take some time , so do the operation in a new thread
*/
public native Bitmap rotateBitmapCcw90(Bitmap bitmap);
...
Bitmap rotatedImage=rotateBitmapCcw90(bitmapToRotate);
编辑:在我得到答案后,我希望与大家分享这段代码和注释:
为了使其正常工作,我在代码中将“uint16_t”的每个实例替换为“uint32_t”(这是我询问过的代码中的错误)。
<输入和输出位图必须是8888配置(即ARGB)
输入的位图将在此过程中被回收。
代码将图像逆时针旋转 90 度。当然你可以根据自己的需要进行更改。
我已经发表了一篇具有此功能和其他功能的精彩帖子,here .
最佳答案
由于您使用的是 ARGB_8888
格式,因此每个像素都是 uint32_t
而不是 uint16_t
。尝试更改您的旋转位图创建以将 uint32_t
用于源数组和目标数组,它应该可以更好地工作。
关于android - 使用 JNI 和 NDK 旋转位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14398670/
我想使用 ndk 构建一个 3rd 方库 avahi。 avahi 已经有 android 端口(带有有效的 Android.mk)。 我做了什么: 我已经成功创建了一个项目并将所有源代码复制到 jn
由于不再需要Android NDK,因此我尝试将其卸载。在Eclipse中找到的路径似乎已损坏。我应该如何清理这个烂摊子?我尝试了brew,但它说未安装NDK。 (它甚至告诉我未安装SDK!也许不是通
我是 Android Studio 和 Android NDK 的新手。我正在尝试使用 android NDK 为类分配编译一个简单的 hello.c 程序。我已按照以下项目说明进行操作,但出现错误:
当我尝试执行 NDK 构建时,我收到以下错误列表...有人知道吗? encies\FreeImage/D:/Projects.CPP/Engine5/svn/trunk/Dependencies/Fr
我正在使用 android-ndk-profiler-3.1 为我的项目进行 NDK 分析。我在 Android.mk 中进行了如下更改... LOCAL_PATH := $(call my-dir)
我需要使用 cutils 库和头文件在我的 NDK 项目中实现 ashmem,但我在我的系统中找不到与 cutils 相关的任何内容。它位于何处或从何处获得? 最佳答案 Android“cutils”
我有两个独立的项目,一个项目用于构建 Boost 库,另一个项目从构建的 boost 库中调用。 Boost 项目编译良好并生成 libboost.a 文件。我想要实现的是将此库添加到我的其他 NDK
我在ndk-build clean上遇到以下错误 mo@mo-Toshiba ~/Workspace/AndroidApp/jni/android_programming_driver $ ndk-b
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在尝试使用 NDK(又使用开放 SLES)在 Android 上创建一个基于流的音频应用程序。我已经完成了基本的流媒体设置。我需要知道 Android 中如何处理欠载(或重载)?错误回调是如何实现
我的 Application.mk 设置为构建 arm 以及 x86 共享库: APP_ABI :- armeabi-v7a x86 我有预建的 openssl 静态库: libcrypto_v7a.
我的 $NDK_HOME 文件夹包含标准库的不同实现。 qdii@nomada /opt/android-ndk/platforms $ ls -l total 24 drwxr-xr-x 5 roo
如何安装android-ndk。我已经为 ndk 设置了路径,但在执行“ndk-build”命令时仍然出现错误。 fya 我正在使用 Windows 操作系统。 最佳答案 它对我有用: 打开命令提示符
构建 Android-ndk 时出现以下错误 请帮助我。 Cygwin.bat @echo off set IS_UNIX= set DEV_ROOT=C:/iliu-Android-NDK-Fibo
我开始使用 android ndk (r8c),为此我安装了新版本的 Ubuntu 12.10 (32bit) 问题,无论我尝试构建什么,我都会遇到让我发疯的怪异错误...... ~/workspac
我正在尝试向我的 Android 项目添加 Crashlytics 支持,该项目使用 NDK 和 gradle CMake。这意味着我的共享项目需要符号。所以如果我想为发布版本创建符号,我会调用 gr
我想知道是否有人设法使用 Android NDK(r8d) 的工具链构建了新的 SDL2。 SDL2 似乎非常接近发布(从昨天开始,它不再是“正在 build 中:http://hg.libsdl.o
我试图在“ndk-build”过程中禁用所有警告,以编译与 Android 上的 JNI 一起使用的 CPP 代码。 我正在使用 LOCAL_CFLAGS := -Wno-error没有成功。 有任何
谁能告诉我如何在android(ndk-build)中建立C-ares库 最佳答案 这是使用NDK standalone toolchain将其构建为ARMv7的静态库的方法: export NDK=
我正在尝试从我的 native 代码中读取一个简单的文本文件。 我把file.txt放在assets文件夹下 在我的事件中,我正在创建 Assets 管理器:assetManager = getAss
我是一名优秀的程序员,十分优秀!