opencv图像格式转换
extern"C" _declspec(dllexport) BYTE* __stdcall ImageConvert(BYTE* imageData, int imageDataSize, const char* format, int* returnSize) {
vector<uchar> data;
for (int i = 0; i < imageDataSize; ++i) {
data.push_back(imageData[i]);
}
印章十不刻cv::Mat image = cv::imdecode(data, CV_LOAD_IMAGE_COLOR);
std::vector<uchar> buf;
imencode(format, image, buf);
*returnSize = buf.size();
BYTE* result = new BYTE[*returnSize];
for (int i = 0; i < *returnSize; i++)
节日庆典result[i] = buf[i];
return result;
}
JAVA调⽤代码:
import java.io.IOException;
唐朝女子妆容import java.nio.file.Files;
import java.nio.file.Paths;
白羊座男生喜欢什么样的女生import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
public class Program {
劲脆鸡腿堡public interface Lib extends Library
{
public Pointer ImageConvert(Pointer data, int dataSize, String format , Pointer resultSize);
}
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
Lib lib = (Lib) Native.loadLibrary("..\\OpenCVDemo.dll", Lib.class);
byte[] data = ("..\\01.tif"));
Pointer ptr = new Memory(data.length);
听课记录怎么写ptr.write(0, data, 0, data.length);
Pointer returnSize = new Memory(4);
Pointer result = lib.ImageConvert(ptr, data.length, ".png" , returnSize);
byte[] resultData = ByteArray(0, Int(0));
Files.("..\\01.png"), resultData);
System.out.println("ok");
}
}
追加:
下⾯这个可能更⾼效:
extern"C" _declspec(dllexport) BYTE* __stdcall ImageConvert(BYTE* imageData, int imageDataSize, const char* format, int* returnSize) {
//convert byte[] to vector<uchar>炒肝的家常做法
std::vector<uchar> data(imageData, imageData + imageDataSize);
//convert image format
cv::Mat image = cv::imdecode(data, CV_LOAD_IMAGE_COLOR);
std::vector<uchar> buf;
imencode(format, image, buf);
双子女和金牛男//covert vector<uchar> to byte[]
*returnSize = buf.size();
BYTE* result = new BYTE[*returnSize];
std::copy(buf.begin(), d(), result);
return result;
}