0

我将 OpenCV 4.1.0 连接到使用 Android Studio 创建 DLL 的 Unity3d 项目(就像在这个 tut LINK中所说的那样)。我想在 Android 平台上使用 DLL 转换灰度纹理。

我在场景中有一个带有纹理的平面游戏对象。

查看场景 查看场景

纹理设置 纹理设置

这里的代码:

C++

extern "C" {
    uint8_t *resultBuffer;
    int bufferWidth;
    int bufferHeight;

    int InitCV_Internal(int width, int height) {
        size_t size = width * height * 4;
        resultBuffer = new uint8_t[size];

        bufferWidth = width;
        bufferHeight = height;

        return 0;
    }

    // Convert the image in grayscale.
    uint8_t *SubmitFrame_Internal(int width, int height, uint8_t *buffer) {
        Mat inFrame = Mat(height, width, CV_8UC4, buffer);
        Mat outFrame(inFrame.rows, inFrame.cols, CV_8UC4, Scalar(0,0,0));

        cvtColor(inFrame, outFrame, COLOR_RGBA2GRAY);

        size_t size = bufferWidth * bufferHeight * 4;
        memcpy(resultBuffer, outFrame.data, size);

        inFrame.release();
        outFrame.release();

        return resultBuffer;
    }

C#

NativeLibAdapter类是处理 DLL 的函数。

using System.Runtime.InteropServices;
using System;
using UnityEngine;

public class NativeLibAdapter {

    [DllImport("native-lib")]
    private static extern int InitCV_Internal(int width, int height);

    [DllImport("native-lib")]
    private static extern IntPtr SubmitFrame_Internal(int width, int height, IntPtr buffer);

    public static int InitCVAdapter(int width, int height)
    {
        int result = InitCV_Internal(width, height);

        return result;
    }

    public static IntPtr SubmitFrameAdapter(int width, int height, IntPtr buffer)
    {
        IntPtr bufferResult = SubmitFrame_Internal(width, height, buffer);

        return bufferResult;
    }
}

测试类附加到场景的平面对象。

using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    private Texture2D texIn;
    private Color32[] pixel32;
    private GCHandle pixelHandle;
    private IntPtr pixelPtr;

    public Text text;

    void Start()
    {
        texIn = (Texture2D)GetComponent<Renderer>().material.mainTexture;
        InitTexture();
    }

    void InitTexture()
    {
        pixel32 = texIn.GetPixels32();
        // Pin pixel32 array
        pixelHandle = GCHandle.Alloc(pixel32, GCHandleType.Pinned);
        // Get the pinned address
        pixelPtr = pixelHandle.AddrOfPinnedObject();

        NativeLibAdapter.InitCVAdapter(texIn.width, texIn.height);

        // MatToTexture2D
        IntPtr result = NativeLibAdapter.SubmitFrameAdapter(texIn.width, texIn.height, pixelPtr);
        int bufferSize = texIn.width * texIn.height * 4;
        byte[] rawData = new byte[bufferSize];

        if (result != IntPtr.Zero)
        {
            Marshal.Copy(result, rawData, 0, bufferSize);
            texIn.LoadRawTextureData(rawData);
            texIn.Apply();
            // text.text = result.ToString();
        }
    }

    void OnApplicationQuit()
    {
        // Free handle
        pixelHandle.Free();
    }
}

构建后 构建后 左侧是我在 android 设备上的结果,右侧是我预期的结果(使用 photoshop 生成)。我插入的文本“-1068498944”仅用于调试。

怎么了?有任何想法吗?太感谢了!

4

0 回答 0