【问题标题】:Android opencv byte[] to mat to byte[]Android opencv byte[] to mat to byte[]
【发布时间】:2016-04-28 21:42:09
【问题描述】:

我的目标是在相机预览上添加一个覆盖,以便找到书本边缘。为此,我在执行以下操作时覆盖了 onPreviewFrame:

public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        int width = parameters.getPreviewSize().width;
        int height = parameters.getPreviewSize().height;
        Mat mat = new Mat((int) (height*1.5), width, CvType.CV_8UC1);
        mat.put(0,0,data);
        byte[] bytes = new byte[(int) (height*width*1.5)];
        mat.get(0,0,bytes);

        if (!test) {        //to only do once
            File pictureFile = getOutputMediaFile();
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(bytes);
                fos.close();
                Uri picUri = Uri.fromFile(pictureFile);
                updateGallery(picUri);
                test = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

现在我只想获取其中一个预览并在转换为 mat 后将其保存。

花了无数个小时使上面看起来正确后,在我的测试手机(LG Leon)上看不到保存的图片。我似乎找不到问题。我是不是因为我在纵向模式下拍照而混合了高度/宽度?我尝试切换它们,但仍然无法正常工作。问题出在哪里?

【问题讨论】:

    标签: android opencv preview


    【解决方案1】:

    在我最近提出的问题中,HERE 描述了我设法找到的最快方法。您可以在我在下面的问题中写的答案中找到提取图像的方法。问题是您通过onPreviewFrame() 获得的图像是NV21。收到这张图片后,可能需要将其转换为 RGB(取决于您要实现的目标;这在我之前给您的答案中也已完成)。

    【讨论】:

    • 很有趣,我的最终目标之一与您的相同:实时显示文本周围的矩形。我会尝试使用你的答案,看看它的去向。谢谢!
    【解决方案2】:

    看起来效率很低,但它对我有用(目前):

    //get the camera parameters
    Camera.Parameters parameters = camera.getParameters();
    int width = parameters.getPreviewSize().width;
    int height = parameters.getPreviewSize().height;
    
    //convert the byte[] to Bitmap through YuvImage; 
    //make sure the previewFormat is NV21 (I set it so somewhere before)
    YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    yuv.compressToJpeg(new Rect(0, 0, width, height), 70, out);
    Bitmap bmp = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
    
    //convert Bitmap to Mat; note the bitmap config ARGB_8888 conversion that 
    //allows you to use other image processing methods and still save at the end
    Mat orig = new Mat();
    bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);
    Utils.bitmapToMat(bmp, orig);
    
    //here you do whatever you want with the Mat
    
    //Mat to Bitmap to OutputStream to byte[] to File
    Utils.matToBitmap(orig, bmp);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 70, stream);
    byte[] bytes = stream.toByteArray();
    File pictureFile = getOutputMediaFile();
    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(bytes);
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-05
      • 2016-11-08
      • 1970-01-01
      • 2012-12-04
      • 2018-04-21
      • 1970-01-01
      • 2011-07-30
      • 2014-02-02
      相关资源
      最近更新 更多