无法打开opencv处理的图像android

时间:2018-07-04 20:08:50

标签: android opencv

我有以下代码段,将图像数据提取到from(dateQuery.first()).pipe( // TAP because it seems to be a sideeffect. If this could stop your stream, "filter" would be appropriate tap(result =>query.greaterThanOrEqualTo('createdAt', result.createdAt)), // MAP because we can discard "result" and only need the Moment-Object in the rest of the stream map(result => getMoment(result.createdAt), // switchMap to switch to the second stream, because it is only one command (in a long pipe) you don´t need a explicit return-command switchMap( createdAt => from(query.find()).pipe( // MAP because we now want a UserObjects-Array in our stream map(el => createUserObjects(el)), // MAP because we want our NamesUpdateList. Depending on the usage you may not need "Observable.of()" here map(userObjects => createNamesUpdateList(userObjects, createdAt )) ) ) ).subscribe( (next) => res.send(serialize(next)), (error) => res.send(serialize(error)), () => console.log('completed') ) getMoment(date){ return moment(result.createdAt).format('DD/MM/YYYY - HH:mm:ss') } createUserObjects(el){ el.map((e) => { return { id: e.id, username: e.get('username') } } } createNamesUpdateList(array, dateFormat){ return { names: array, lastUpdate: dateFormat } } 对象中,然后绘制一个圆。之后,我尝试将处理后的数据放回Mat数组中。该文件已保存,但是当我随后打开它时,收到消息,提示它无法打开。

byte[]

如Micka所建议,可能可以使用imwrite。但是,如果要在捕获图像之前在屏幕上显示经过处理的图像怎么办?然后,我必须将已处理的private static class ImageSaver implements Runnable { /** * The JPEG image */ private final Image mImage; private final File mFile; ImageSaver(Image image, File file){ mImage = image; mFile = file; } @Override public void run(){ ByteBuffer buffer = mImage.getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); Mat mat = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED); Point pt = new Point(mat.width()/2, mat.height()/2); int radius = 40; Scalar color = new Scalar(256,0,0); Imgproc.circle(mat, pt, radius, color); int length = (int) (mat.total() * mat.elemSize()); byte[] newBytes = new byte[length]; mat.get(0,0,newBytes); FileOutputStream output = null; try { output = new FileOutputStream(mFile); output.write(newBytes); } catch (IOException e){ e.printStackTrace(); } finally { mImage.close(); if (output != null){ try { output.close(); } catch (IOException e){ e.printStackTrace(); } } } } } 复制到bytes吗?

我想改用本机,因为这看起来更快,分辨率更高。

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了自己的问题的答案。首先,如果保存图像是唯一的目标,那么使用imwrite确实是最简单的解决方案。

一种替代方法是使用

MatOfByte mByte = new MatOfByte();
Imgcodecs.imencode(".jpeg", mat, mByte);
...
output.write(mByte.toArray());
...