2016-10-04 213 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

相關問題