1. 程式人生 > >android全平臺編譯libpng並基於ANativeWindow載入PNG圖片

android全平臺編譯libpng並基於ANativeWindow載入PNG圖片

圖形影象實踐

環境配置

作業系統:ubuntu 16.05
ndk版本:android-ndk-r16b
libpng版本:libpng-1.6.35
zlib版本:zlib-1.2.11

進入libpng-1.6.35/scripts目錄,將pnglibconf.h.prebuilt複製到libpng-1.6.35目錄下,並重命名為pnglibconf.h

開始執行ndk-build

載入PNG圖片

新建native-png工程

新建NativePngLoader類

package com.onzhou.graphic.png;

import android.view.Surface;

public class NativePngLoader {

    static {
        System.loadLibrary("native-png");
    }

    public native void loadPNGImage(String imagePath, Surface surface);


}

新建native_png.cpp的native實現類

#include <jni.h>
#include <stdio.h>
#include <time.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>

#include "native_png.h"
#include "png.h"
#include "zlib.h"
#include <malloc.h>
#include <string.h>

#ifdef ANDROID

#include <
android/log.h> #define LOG_TAG "NativePNG" #define LOGE(format, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, format, ##__VA_ARGS__) #define LOGI(format, ...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, format, ##__VA_ARGS__) #else #define LOGE(format, ...) printf(LOG_TAG format "\n", ##__VA_ARGS__) #define LOGI(format, ...) printf(LOG_TAG format "\n", ##__VA_ARGS__) #endif /** * 動態註冊 */ JNINativeMethod methods[] = { {"loadPNGImage", "(Ljava/lang/String;Landroid/view/Surface;)V", (void *) loadPNGImage} }; /** * 動態註冊 * @param env * @return */ jint registerNativeMethod(JNIEnv *env) { jclass cl = env->FindClass("com/onzhou/graphic/png/NativePngLoader"); if ((env->RegisterNatives(cl, methods, sizeof(methods) / sizeof(methods[0]))) < 0) { return -1; } return 0; } /** * 載入預設回撥 * @param vm * @param reserved * @return */ jint JNI_OnLoad(JavaVM *vm, void *reserved) { JNIEnv *env = NULL; if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) { return -1; } //註冊方法 if (registerNativeMethod(env) != JNI_OK) { return -1; } return JNI_VERSION_1_6; } void ThrowException(JNIEnv *env, const char *exception, const char *message) { jclass clazz = env->FindClass(exception); if (NULL != clazz) { env->ThrowNew(clazz, message); } } void drawPNG(const char *name, ANativeWindow_Buffer &nwBuffer) { FILE *file = fopen(name, "rb"); png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); if (png_ptr == NULL) { fclose(file); return; } png_infop info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { fclose(file); png_destroy_write_struct(&png_ptr, NULL); return; } if (setjmp(png_jmpbuf(png_ptr))) { fclose(file); png_destroy_write_struct(&png_ptr, &info_ptr); return; } //開始讀檔案 png_init_io(png_ptr, file); png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND, 0); //獲取檔案的寬高色深 int m_width = png_get_image_width(png_ptr, info_ptr); int m_height = png_get_image_height(png_ptr, info_ptr); //獲取影象的色彩型別 int color_type = png_get_color_type(png_ptr, info_ptr); LOGI("width=%d,height=%d,color_type=%d", m_width, m_height, color_type); // row_pointers內部存放的就是RGBA資料了 png_bytep *row_pointers = png_get_rows(png_ptr, info_ptr); switch (color_type) { case PNG_COLOR_TYPE_RGB_ALPHA: //RGBA32 break; case PNG_COLOR_TYPE_RGB: { //RGB24,沒有A通道,就要3位3位的讀 uint32_t *line = (uint32_t *) nwBuffer.bits; for (int row = 0; row < m_height; row++) { for (int column = 0; column < m_width; column++) { //儲存順序為BGR,BGR,BGR...... line[column] = ((uint32_t) row_pointers[row][3 * column + 2]) << 16 | ((uint32_t) row_pointers[row][3 * column + 1]) << 8 | (uint32_t) row_pointers[row][3 * column]; } line = line + nwBuffer.stride; } } } png_destroy_read_struct(&png_ptr, &info_ptr, 0); fclose(file); } void loadPNGImage(JNIEnv *env, jobject obj, jstring jpegPath, jobject surface) { const char *path = env->GetStringUTFChars(jpegPath, 0); //獲取目標surface ANativeWindow *window = ANativeWindow_fromSurface(env, surface); if (NULL == window) { ThrowException(env, "java/lang/RuntimeException", "unable to get native window"); return; } //預設的是RGB_565 int32_t result = ANativeWindow_setBuffersGeometry(window, 0, 0, WINDOW_FORMAT_RGBA_8888); if (result < 0) { ThrowException(env, "java/lang/RuntimeException", "unable to set buffers geometry"); //釋放視窗 ANativeWindow_release(window); window = NULL; return; } ANativeWindow_acquire(window); ANativeWindow_Buffer buffer; //鎖定視窗的繪圖表面 if (ANativeWindow_lock(window, &buffer, NULL) < 0) { ThrowException(env, "java/lang/RuntimeException", "unable to lock native window"); //釋放視窗 ANativeWindow_release(window); window = NULL; return; } //繪製PNG圖片 drawPNG(path, buffer); //解鎖視窗的繪圖表面 if (ANativeWindow_unlockAndPost(window) < 0) { ThrowException(env, "java/lang/RuntimeException", "unable to unlock and post to native window"); } env->ReleaseStringUTFChars(jpegPath, path); //釋放 ANativeWindow_release(window); }

注意:上面的過程其實也比較簡單,但是跟讀取JPEG圖片區別在於要根據色彩來區分RGBRGBA

  • 先通過ANativeWindow_fromSurface獲取對應的視窗
  • 通過libpng載入對應的png圖片,解碼成RGBA資料
  • 將最終的RGBA資料寫入到buffer中去,完成繪製

編寫cmake的配置檔案CMakeLists.txt


cmake_minimum_required(VERSION 3.4.1)

##官方標準配置
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti -fno-exceptions -Wall")

add_library(native-png
           SHARED
           src/main/cpp/native_png.cpp)

add_library(png
           SHARED
           IMPORTED)

set_target_properties(png
                    PROPERTIES IMPORTED_LOCATION
                    ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libpng.so
                    )

add_library(zip
           SHARED
           IMPORTED)

set_target_properties(zip
                    PROPERTIES IMPORTED_LOCATION
                    ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libz.so
                    )

#標頭檔案
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include_png)
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include_zlib)

target_link_libraries(native-png
            png
            zip
            android
            jnigraphics
            log)

在啟動的目標Activity中載入我們指定的JPEG圖片

mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        NativeImageLoader nativeImageLoader = new NativeImageLoader();
        File file = new File(getExternalFilesDir(null), "input.jpeg");
        nativeImageLoader.loadJPEGImage(file.getAbsolutePath(), holder.getSurface());
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
});

可以看到如下輸出:

專案地址:native-png
https://github.com/byhook/graphic4android

參考:
https://blog.csdn.net/talkxin/article/details/50968313