2016-10-04 212 views
0

我正在创建一个简单的YUV到JPEG图像转换器。为此,我需要从文件中获取YUV图像的大小。我很难在这里进行测试。但我需要从语法上获得这些细节。任何帮助将升值。如何从Android中的YUV图像文件获取宽度和高度?

private void covertToYuvImage() { 

    ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); 
    String yuvImagePath = selectedFolder+yuvImageList.get(0); //Path to YUV image 
    File file = new File(yuvImagePath); 
    byte[] bFile = new byte[(int) file.length()]; 
    try{ 
     FileInputStream fileInputStream = new FileInputStream(file); 
     fileInputStream.read(bFile); 
     fileInputStream.close(); 
    } 
    catch (Exception e){ 
     e.printStackTrace(); 
    } 

    YuvImage yuvImage = new YuvImage(bFile, ImageFormat.NV21,2048,1536,null); 
    yuvImage.compressToJpeg(new Rect(0, 0, 2048, 1536), 100, baoStream); 

    File imgFile = new File(selectedFolder, "NewImage.jpeg"); 
    if (!imgFile.exists()) { 

     FileOutputStream img; 
     try { 
      img = new FileOutputStream(imgFile); 
      img.write(baoStream.toByteArray()); 
      img.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

但输出是可怕的。请参阅附件图片。 enter image description here

我该如何解决这个问题?

+0

我的天堂”尝试过,但似乎这个答案会帮助你,如果你对位图进行操作,而不是从文件中选择图像。 http://stackoverflow.com/questions/5960247/convert-bitmap-array-to-yuv-ycbcr-nv21 – VVB

回答

0
YuvImage yuvImage = new YuvImage(bFile, ImageFormat.NV21,2048,1536,null); 
yuvImage.compressToJpeg(new Rect(0, 0, 2048, 1536), 100, baoStream); 
yuvImage.getWidth() // it will return width in int 
yuvImagegetHeight() // It will return height in int 

Try this link希望这将帮助你

+0

这将返回2048和1536 ..但我想知道是否有可能从YUV图像中获取这些值可以在YuvImage的构造函数中使用的文件 – WEAPI

+0

你对输出图像有任何想法 – WEAPI

+0

希望这个链接可以帮助你http://stackoverflow.com/questions/9192982/displaying-yuv-image-in-android – Moorthy

相关问题