簡體   English   中英

Android-將ARGB_8888位圖轉換為3BYTE_BGR

[英]Android- convert ARGB_8888 bitmap to 3BYTE_BGR

通過執行以下操作,我得到了ARGB_8888位圖的像素數據:

public void getImagePixels(byte[] pixels, Bitmap image) {
    // calculate how many bytes our image consists of
    int bytes = image.getByteCount();

    ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
    image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer

    pixels = buffer.array(); // Get the underlying array containing the data.
}

但是,我想將每個像素存儲在四個字節(ARGB)中的數據轉換為每個像素存儲在3個字節( BGR )中的數據。
任何幫助表示贊賞!

免責聲明:使用Android Bitmap API可能會有更好/更輕松/更快的方法,但是我對此並不熟悉。 如果您想沿着開始的方向前進,請修改以下代碼,將4字節ARGB轉換為3字節BGR

public byte[] getImagePixels(Bitmap image) {
    // calculate how many bytes our image consists of
    int bytes = image.getByteCount();

    ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
    image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer

    byte[] temp = buffer.array(); // Get the underlying array containing the data.

    byte[] pixels = new byte[(temp.length / 4) * 3]; // Allocate for 3 byte BGR

    // Copy pixels into place
    for (int i = 0; i < (temp.length / 4); i++) {
       pixels[i * 3] = temp[i * 4 + 3];     // B
       pixels[i * 3 + 1] = temp[i * 4 + 2]; // G
       pixels[i * 3 + 2] = temp[i * 4 + 1]; // R

       // Alpha is discarded
    }

    return pixels;
}

您可以使用名為OpenCV的強大庫來嘗試您的技能-它是免費的。 它允許您從BGR更改為RGB,然后反轉。 它還允許您添加或刪除Alpha通道(“ A”)。

OpenCV有專用的Android版本可在此處下載(適用於Android v.4.6的OpenCV)

在該庫中,從本文檔中檢查cvtColor() ,其中指出以下內容:

該函數可以執行以下轉換:在RGB空間內進行轉換,例如添加/刪除alpha通道,反轉通道順序,與16位RGB顏色(R5:G6:B5或R5:G5:B5)之間的轉換。作為與灰度之間的轉換[...等]

我在Google Play商店( UnCanny )中有一個使用Android OpenCV的應用程序。 它花了一些時間來加快速度,但具有大量功能。

使用OpenCV庫,您可以通過不同的方法獲取像素(請參見下文),您可以將Java函數替換為本地調用,並且速度要快約4倍

綜上所述:

// reading bitmap from java side:
Mat mFrame = Mat(height,width,CV_8UC4,pFrameData).clone();
Mat mout;
cvtColor(mFrame, mout,CV_RGB2GRAY); // or CV_RGB2XXX (3BGR)

完整的例子:

Java方面:

    Bitmap bitmap = mTextureView.getBitmap(mWidth, mHeight);
    int[] argb = new int[mWidth * mHeight];
    // get ARGB pixels and then proccess it with 8UC4 opencv convertion
    bitmap.getPixels(argb, 0, mWidth, 0, 0, mWidth, mHeight);
    // native method (NDK or CMake)
    processFrame8UC4(argb, mWidth, mHeight);

本機端(NDK):

JNIEXPORT jint JNICALL com_native_detector_Utils_processFrame8UC4
    (JNIEnv *env, jobject object, jint width, jint height, jintArray frame) {

    jint *pFrameData = env->GetIntArrayElements(frame, 0);
    // it is the line:
    Mat mFrame = Mat(height,width,CV_8UC4,pFrameData).clone();
    // the next only is a extra example to gray convertion:
    Mat mout;
    cvtColor(mFrame, mout,CV_RGB2GRAY); // or CV_RGB2XXX
    // your code from here, the next is a example:
    int objects = face_detection(env, mout);
    // release object
    env->ReleaseIntArrayElements(frame, pFrameData, 0);
    return objects;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM