获取像素颜色 - GetColorPtr
函数简介
从指定的图片中获取指定坐标 (x, y)
处的颜色值,颜色返回格式"AARRGGBB"
函数原型
long GetColorPtr(long ola, long source, int x, int y);
参数定义
ola
: OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。source
: 源对象的指针,通常是一个图像或画布对象。x
: 要获取颜色的像素点的横坐标。y
: 要获取颜色的像素点的纵坐标。
返回值
- 返回值:颜色返回格式"AARRGGBB"
示例
SDK
C#
using System;
using OLA.ServiceCenter.PlugFactory;
namespace OLADemo
{
internal class Program
{
static OLAPlugServer OLAServer;
static void Main(string[] args)
{
OLAServer = new OLAPlugServer();
var regResult = OLAServer.Reg(
OLAServer.UserCode,
OLAServer.SoftCode,
OLAServer.FeatureList
);
OLAServer.CreateCOLAPlugInterFace();
long source = OLAServer.LoadImage("image.png"); // 假设有一个加载图像的函数
Console.WriteLine($"LoadImage 返回:{source}");
// 获取颜色指针
int x = 100;
int y = 200;
long colorPtr = OLAServer.GetColorPtr(source, x, y);
if (colorPtr != 0)
{
// 假设颜色值是一个32位的整数(ARGB格式)
int colorValue = Marshal.ReadInt32(new IntPtr(colorPtr));
Console.WriteLine($"坐标 ({x}, {y}) 处的颜色值: {colorValue}");
}
else
{
Console.WriteLine("获取颜色失败。");
}
}
}
}
Python
from OLAPlugServer import OLAPlugServer
# 实例化
OLAServer = OLAPlugServer()
# 注册
OLAServer.Reg(OLAServer.UserCode, OLAServer.SoftCode, OLAServer.FeatureList)
# 创建OLAPlug对象
OLAServer.CreateCOLAPlugInterFace()
# 加载图像
source = OLAServer.LoadImage("image.png") # 假设有一个加载图像的函数
print(f"LoadImage 返回: {source}")
# 获取颜色指针
x = 100
y = 200
colorPtr = OLAServer.GetColorPtr(source, x, y)
if colorPtr != 0:
# 假设颜色值是一个32位的整数(ARGB格式)
colorValue = ctypes.cast(colorPtr, ctypes.POINTER(ctypes.c_int32)).contents.value
print(f"坐标 ({x}, {y}) 处的颜色值: {colorValue}")
else:
print("获取颜色失败。")
原生方式
Python
import os
import sys
from ctypes import *
# 1. 加载dll
# 此处路径为插件所在路径,请根据实际情况修改。
# 32位python使用x86版本,64位python使用x64版本
if sys.maxsize > 2**32:
olaplug_dll = WinDLL(os.path.abspath(os.path.join(os.getcwd(), 'OLAPlug_x64.dll')))
else:
olaplug_dll = WinDLL(os.path.abspath(os.path.join(os.getcwd(), 'OLAPlug_x86.dll')))
# 2. 注册到后台
UserCode = "c38e200f116d4fa8bd0deb45ccb523ea"
SoftCode = "701bc92ba84642c68845e7a06c10fd99"
FeatureList = "OLA|OLAPlus"
olaplug_dll.Reg.argtypes = [c_char_p, c_char_p, c_char_p]
olaplug_dll.Reg.restype = c_int32
result = olaplug_dll.Reg(UserCode.encode('utf-8'), SoftCode.encode('utf-8'), FeatureList.encode('utf-8'))
print(f'注册结果返回: {result}')
# 3. 创建ola对象
olaplug_dll.CreateCOLAPlugInterFace.restype = c_void_p
ola_obj = olaplug_dll.CreateCOLAPlugInterFace()
# 4. 加载图像
olaplug_dll.LoadImage.argtypes = [c_void_p, c_char_p]
olaplug_dll.LoadImage.restype = c_void_p
source = olaplug_dll.LoadImage(ola_obj, "image.png".encode('utf-8'))
print(f"LoadImage 返回: {source}")
# 5. 获取颜色指针
x = 100
y = 200
olaplug_dll.GetColorPtr.argtypes = [c_void_p, c_void_p, c_int32, c_int32]
olaplug_dll.GetColorPtr.restype = c_void_p
colorPtr = olaplug_dll.GetColorPtr(ola_obj, source, x, y)
if colorPtr != 0:
# 假设颜色值是一个32位的整数(ARGB格式)
colorValue = ctypes.cast(colorPtr, ctypes.POINTER(ctypes.c_int32)).contents.value
print(f"坐标 ({x}, {y}) 处的颜色值: {colorValue}")
else:
print("获取颜色失败。")
返回值
字符串: 颜色字符串(注意这里都是小写字符,和工具相匹配)
注意:
DLL调用返回字符串指针地址,需要调用 FreeStringPtr接口释放内存