Siva-Karthi / webp

Automatically exported from code.google.com/p/webp
0 stars 0 forks source link

i try to convert JPG to WEBP by webp-imageio ,But fail . Can someone help me? #171

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I follow the following operations
first:

https://bitbucket.org/luciad/webp-imageio by this i get jar webp-imageio.jar 
and webp-imageio.so

second :

   i create a test like this:

public class TestWebp {

    private static boolean NATIVE_LIBRARY_LOADED = false;

    static synchronized void loadNativeLibrary() {
        if (!NATIVE_LIBRARY_LOADED) {
            NATIVE_LIBRARY_LOADED = true;
            System.loadLibrary("webp-imageio");
        }
    }

    static {
        loadNativeLibrary();
    }

    public static void main(String[] args) throws Exception {
        // read image file
        String f = "/home/caoyaojun/Pictures/1353735742546_original.jpg";
        File file = new File(f);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] orginabyte = new byte[200000000];
        fileInputStream.read(orginabyte);
        BufferedImage image = getImage(orginabyte);
        // compression
        byte[] optimizedData = optimize(0.8F, "webp", image);
        // write image file
        FileOutputStream out = new FileOutputStream("/home/caoyaojun/Pictures/test.webp");
        out.write(optimizedData);
        out.flush();
        out.close();
    }

    /**
     * get image
     * 
     * @param data
     * @return
     * @throws IOException
     */
    private static BufferedImage getImage(byte[] data) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
        BufferedImage image = null;
        try {
            image = ImageIO.read(byteArrayInputStream);
            return image;
        } catch (IOException e) {
            throw new IOException("read image exception!", e);
        } finally {
            try {
                byteArrayInputStream.close();
            } catch (IOException e) {
            }
        }
    }

    private static byte[] optimize(Float compressionQuality, String extension, BufferedImage image) throws Exception {
        // get writer
        ImageWriter imgWrier = ImageIO.getImageWritersByFormatName(extension).next();
        ImageWriteParam imgWriteParams = new WebPWriteParam(null);
        imgWriteParams.setCompressionType("Lossy");
        // 0~1
        imgWriteParams.setCompressionQuality(compressionQuality);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
            imgWrier.setOutput(imageOutputStream);
            imgWrier.write(null, new IIOImage(image, null, null), imgWriteParams);
            int length = (int) imageOutputStream.length();
            byte[] resultDate = new byte[length];
            imageOutputStream.read(resultDate, 0, length);
            return resultDate;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
            }
        }
    }
}

i can get file, and not empty. but i can not open with chrome;
the  operating system is ubuntu 13.04

can somebody helpe me?

Original issue reported on code.google.com by caoyaoju...@gmail.com on 7 Sep 2013 at 9:40

GoogleCodeExporter commented 9 years ago
can you open it with 'vwebp' tool? Does it display correctly?

What does 'webpmux -info' or 'dwebp' say about this file? Is it a valid one?

Original comment by pascal.m...@gmail.com on 29 Jan 2014 at 9:21

GoogleCodeExporter commented 9 years ago
i meet the same problem, the output file's header is not like a webp image.

Original comment by macma...@gmail.com on 21 Aug 2014 at 11:17