Status: New
Owner: ----
Labels: Type-Defect Priority-Medium
New issue 171 by
caoyaoju...@gmail.com: i try to convert JPG to WEBP by
webp-imageio ,But fail . Can someone help me?
http://code.google.com/p/webp/issues/detail?id=171
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?
--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings