0

I am creating a simple YUV to JPEG image converter.for that I need to get the size of YUV image from the file. I hard corded here for testing. but I need to get those details grammatically. Any help will appreciate.

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();
        }
    }
}

But output was horrible. Please see attached image. enter image description here

How can I solve this issue ?

WEAPI
  • 101
  • 4
  • 9
  • I haven' tried it but seems this answer will help you if you perform operation on Bitmap instead of picking image from file. http://stackoverflow.com/questions/5960247/convert-bitmap-array-to-yuv-ycbcr-nv21 – VVB Oct 04 '16 at 06:13

1 Answers1

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 hope this will help you

Moorthy
  • 723
  • 1
  • 6
  • 20
  • This will return 2048 and 1536.. but I want to know whether it is possible to get those values from YUV image file which can be used in constructor of YuvImage – WEAPI Oct 04 '16 at 05:30
  • Do you have any idea about output image – WEAPI Oct 04 '16 at 05:30
  • hope this link will help you http://stackoverflow.com/questions/9192982/displaying-yuv-image-in-android – Moorthy Oct 04 '16 at 06:03