Mat到字节[]的转换在java中不起作用
我尝试在java中将Mat转换为字节数组。将Mat转换为字节[]和字节[]转换为Mat时,我无法保留Mat的原始值。get接受字节[]作为不起作用的参数。有人能帮我做同样的事吗 用例:抓取每一帧视频并通过卡夫卡将其发送给包含字节[]消息的消费者,然后消费者将接收字节数组并转换为Mat并将其保存为图像 我在java中访问过类似的帖子,但没有找到解决方案 请参阅我的代码:Mat到字节[]的转换在java中不起作用,java,opencv,Java,Opencv,我尝试在java中将Mat转换为字节数组。将Mat转换为字节[]和字节[]转换为Mat时,我无法保留Mat的原始值。get接受字节[]作为不起作用的参数。有人能帮我做同样的事吗 用例:抓取每一帧视频并通过卡夫卡将其发送给包含字节[]消息的消费者,然后消费者将接收字节数组并转换为Mat并将其保存为图像 我在java中访问过类似的帖子,但没有找到解决方案 请参阅我的代码: ` System.loadLibrary("opencv_java249"); MatOfByte
` System.loadLibrary("opencv_java249");
MatOfByte webcam_image = new MatOfByte();
VideoCapture capture = new VideoCapture(
"/home/jishnu/CodeT/TrainingDataSet/Video.mp4");
System.out.println("Frame Grabber started");
byte[] frameArray;
int i=0;
if (capture.isOpened()) {
while (true) {
capture.read(webcam_image);
frameArray = new byte[(int) (webcam_image.total() * webcam_image
.channels())];
if (!webcam_image.empty()) {
// System.out.print(".");
webcam_image.get(0,0,frameArray);
producer.send(new KeyedMessage<String, byte[]>("imageTopic",frameArray));
//Below statements are only for debugging
System.out.println(frameArray.length);
MatOfByte inputframe=new MatOfByte(frameArray);
boolean b= Highgui.imwrite("/home/jishnu/CodeT/Today7.jpeg", inputframe);
if(b){System.out.println("save image success");}
else System.out.println("save image failed");
inputframe.fromArray(frameArray);
b= Highgui.imwrite("/home/jishnu/CodeT/Today6.bmp",inputframe);
if(b){System.out.println("save image success");System.exit(0);}
else System.out.println("save image failed");
} else {
System.out.println(" --(!) No captured frame -- Break!");
`System.loadLibrary(“opencv_java249”);
MatOfByte网络摄像头_image=新的MatOfByte();
视频捕获=新视频捕获(
“/home/jishnu/CodeT/TrainingDataSet/Video.mp4”);
System.out.println(“帧抓取器启动”);
字节[]帧数组;
int i=0;
if(capture.isOpened()){
while(true){
捕获、读取(网络摄像头图像);
frameArray=新字节[(int)(网络摄像头图像.total()*网络摄像头图像
.channels())];
如果(!webcam_image.empty()){
//系统输出打印(“.”);
网络摄像头图像获取(0,0,帧阵列);
send(新的KeyedMessage(“imageTopic”,frameArray));
//以下语句仅用于调试
System.out.println(frameArray.length);
MatOfByte inputframe=新的MatOfByte(帧数组);
布尔b=Highgui.imwrite(“/home/jishnu/CodeT/Today7.jpeg”,inputframe);
if(b){System.out.println(“保存图像成功”);}
else System.out.println(“保存图像失败”);
inputframe.fromArray(frameArray);
b=Highgui.imwrite(“/home/jishnu/CodeT/Today6.bmp”,inputframe);
如果(b){System.out.println(“保存图像成功”);System.exit(0);}
else System.out.println(“保存图像失败”);
}否则{
System.out.println(“--(!)未捕获帧--Break!”);
`如果有人遇到同样的错误。 我使用
Mat.put(frameArray)
从字节创建Mat,而不是MatOfByte inputframe=new MatOfByte(frameArray);
现在一切正常,我不知道为什么MatofByte构造函数不工作
已在JavaCV的更高版本中修复。我正在使用它。我用于从文件进行写入和读取
public static void save(Mat mat, String name)
{
File file = new File(path, name);
int length = (int) (mat.total() * mat.elemSize());
byte buffer[] = new byte[length];
mat.get(0, 0, buffer);
try
{
FileUtils.writeByteArrayToFile(file, buffer);
} catch (IOException e)
{
e.printStackTrace();
}
}
public static Mat load(String name)
{
File file = new File(path, name);
byte[] buffer = new byte[0];
try
{
buffer = FileUtils.readFileToByteArray(file);
} catch (IOException e)
{
e.printStackTrace();
}
Mat mat = new Mat(row, col, type);
mat.put(0, 0, buffer);
return mat;
}
OpenCV版本:4.5.1
重构:
public static void main(String[] args) throws IOException {
String filePath = ".jpg";
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Mat mat=Imgcodecs.imread(filePath);
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", mat, matOfByte);
byte[] byteArray = matOfByte.toArray();
Path path = Paths.get("D:\\yyy2.jpg");
Files.write(path, byteArray);
//InputStream inputStream = new ByteArrayInputStream(byteArray);
}