我试图在Mat中将Mat转换为字节数组.在将Mat转换为Byte []和Byte []转换为Mat时,我无法保留Mat的原始值.Mat .get接受byte []作为不起作用的参数.任何人都可以帮助我吗?
使用案例:要获取每个视频帧并通过kafka将其发送到包含byte []消息的消费者,然后消费者将收到字节数组并转换为Mat并将其保存为图像.
我在java中遇到了类似的帖子,但没有找到解决方案.这里
看我的代码:
` 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!");
Run Code Online (Sandbox Code Playgroud)
`
这对我有用.我使用apachi-common进行写入和从文件中读取.
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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6612 次 |
最近记录: |