Trong lúc lập trình xử lý hình ảnh dạng tif, tôi thường gặp lỗi "Bug One factory fails for the operation encode".
Sau khi tìm tòi trên các diễn đàn trên internet, thì biết được nguyên nhân gây ra lỗi là do bộ mã loại của hình ảnh(type image encode) không được hỗ trợ để xử lý dạng hình tiff.
Do trên internet không giải thíc cách xử lý bug này, nên sau một hồi dài ngồi ngâm lại code thì tôi đã tìm được giải pháp.
Bài viết này tôi muốn chia sẻ kinh nghiêm của mình đối với bug này, rất mong được sự đóng góp của các bạn có gặp trường hợp tương tự như của tôi.
Dưới đây là đoạn lỗi tôi gặp phải.
Error: One factory fails for the operation "encode"
Occurs in: javax.media.jai.ThreadSafeOperationRegistry
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at com.sun.media.jai.opimage.FileStoreRIF.create(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at javax.media.jai.RenderedOp.createInstance(Unknown Source)
at javax.media.jai.RenderedOp.createRendering(Unknown Source)
at javax.media.jai.RenderedOp.getRendering(Unknown Source)
at javax.media.jai.JAI.createNS(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at TestTiff.doitJAI(TestTiff.java:37)
at TestTiff.main(TestTiff.java:17)
Caused by: java.lang.Error: Bilevel encodings are supported for bilevel images only.
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.opimage.EncodeRIF.create(Unknown Source)
... 25 more
Error: One factory fails for the operation "filestore"
Occurs in: javax.media.jai.ThreadSafeOperationRegistry
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at javax.media.jai.RenderedOp.createInstance(Unknown Source)
at javax.media.jai.RenderedOp.createRendering(Unknown Source)
at javax.media.jai.RenderedOp.getRendering(Unknown Source)
at javax.media.jai.JAI.createNS(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at TestTiff.doitJAI(TestTiff.java:37)
at TestTiff.main(TestTiff.java:17)
Caused by: javax.media.jai.util.ImagingException: All factories fail for the operation "encode"
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at com.sun.media.jai.opimage.FileStoreRIF.create(Unknown Source)
... 16 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
... 20 more
Caused by: java.lang.Error: Bilevel encodings are supported for bilevel images only.
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.opimage.EncodeRIF.create(Unknown Source)
... 25 more
Exception in thread "main" javax.media.jai.util.ImagingException: All factories fail for the operation "filestore"
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at javax.media.jai.RenderedOp.createInstance(Unknown Source)
at javax.media.jai.RenderedOp.createRendering(Unknown Source)
at javax.media.jai.RenderedOp.getRendering(Unknown Source)
at javax.media.jai.JAI.createNS(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at TestTiff.doitJAI(TestTiff.java:37)
at TestTiff.main(TestTiff.java:17)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
... 11 more
Caused by: javax.media.jai.util.ImagingException: All factories fail for the operation "encode"
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at com.sun.media.jai.opimage.FileStoreRIF.create(Unknown Source)
... 16 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
... 20 more
Caused by: java.lang.Error: Bilevel encodings are supported for bilevel images only.
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.opimage.EncodeRIF.create(Unknown Source)
... 25 more
Caused by:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at javax.media.jai.RenderedOp.createInstance(Unknown Source)
at javax.media.jai.RenderedOp.createRendering(Unknown Source)
at javax.media.jai.RenderedOp.getRendering(Unknown Source)
at javax.media.jai.JAI.createNS(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at TestTiff.doitJAI(TestTiff.java:37)
at TestTiff.main(TestTiff.java:17)
Caused by: javax.media.jai.util.ImagingException: All factories fail for the operation "encode"
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at com.sun.media.jai.opimage.FileStoreRIF.create(Unknown Source)
... 16 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
... 20 more
Caused by: java.lang.Error: Bilevel encodings are supported for bilevel images only.
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.opimage.EncodeRIF.create(Unknown Source)
... 25 more
Occurs in: javax.media.jai.ThreadSafeOperationRegistry
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at com.sun.media.jai.opimage.FileStoreRIF.create(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at javax.media.jai.RenderedOp.createInstance(Unknown Source)
at javax.media.jai.RenderedOp.createRendering(Unknown Source)
at javax.media.jai.RenderedOp.getRendering(Unknown Source)
at javax.media.jai.JAI.createNS(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at TestTiff.doitJAI(TestTiff.java:37)
at TestTiff.main(TestTiff.java:17)
Caused by: java.lang.Error: Bilevel encodings are supported for bilevel images only.
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.opimage.EncodeRIF.create(Unknown Source)
... 25 more
Error: One factory fails for the operation "filestore"
Occurs in: javax.media.jai.ThreadSafeOperationRegistry
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at javax.media.jai.RenderedOp.createInstance(Unknown Source)
at javax.media.jai.RenderedOp.createRendering(Unknown Source)
at javax.media.jai.RenderedOp.getRendering(Unknown Source)
at javax.media.jai.JAI.createNS(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at TestTiff.doitJAI(TestTiff.java:37)
at TestTiff.main(TestTiff.java:17)
Caused by: javax.media.jai.util.ImagingException: All factories fail for the operation "encode"
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at com.sun.media.jai.opimage.FileStoreRIF.create(Unknown Source)
... 16 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
... 20 more
Caused by: java.lang.Error: Bilevel encodings are supported for bilevel images only.
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.opimage.EncodeRIF.create(Unknown Source)
... 25 more
Exception in thread "main" javax.media.jai.util.ImagingException: All factories fail for the operation "filestore"
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at javax.media.jai.RenderedOp.createInstance(Unknown Source)
at javax.media.jai.RenderedOp.createRendering(Unknown Source)
at javax.media.jai.RenderedOp.getRendering(Unknown Source)
at javax.media.jai.JAI.createNS(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at TestTiff.doitJAI(TestTiff.java:37)
at TestTiff.main(TestTiff.java:17)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
... 11 more
Caused by: javax.media.jai.util.ImagingException: All factories fail for the operation "encode"
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at com.sun.media.jai.opimage.FileStoreRIF.create(Unknown Source)
... 16 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
... 20 more
Caused by: java.lang.Error: Bilevel encodings are supported for bilevel images only.
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.opimage.EncodeRIF.create(Unknown Source)
... 25 more
Caused by:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at javax.media.jai.RenderedOp.createInstance(Unknown Source)
at javax.media.jai.RenderedOp.createRendering(Unknown Source)
at javax.media.jai.RenderedOp.getRendering(Unknown Source)
at javax.media.jai.JAI.createNS(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at javax.media.jai.JAI.create(Unknown Source)
at TestTiff.doitJAI(TestTiff.java:37)
at TestTiff.main(TestTiff.java:17)
Caused by: javax.media.jai.util.ImagingException: All factories fail for the operation "encode"
at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
at com.sun.media.jai.opimage.FileStoreRIF.create(Unknown Source)
... 16 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(Unknown Source)
... 20 more
Caused by: java.lang.Error: Bilevel encodings are supported for bilevel images only.
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.codecimpl.TIFFImageEncoder.encode(Unknown Source)
at com.sun.media.jai.opimage.EncodeRIF.create(Unknown Source)
... 25 more
Trước khi xử lý bug này ta cần hiểu tập tin TIF là một dạng file dùng để chứa(hoặc nén) nhiều hình lại thành một tập tin duy nhất. Hiểu đơn giản thì nó giống như thùng chứa hình(container) vậy.
Theo thông tin tôi tìm hiểu được thì tập tin TIF sẽ có các dạng nén sau:
- None
- LZW
- Packbits
- JPEG
- ZIP
- Huffman RLE
- CCITT Fax 3
- CCITT Fax 4
Theo định nghĩa trên nên khi xử lý hình TIF nếu ta chọn sai loại kiểu nén(compression) của hình ảnh thì sẽ dẫn đến lỗi "Bug One factory fails for the operation encode."
Như lỗi phát sinh tôi gặp phải được phát sinh bởi đoạn mã sau:
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
Ở đây hình TIF tôi dùng để xử lý có dáng nén(compression) là JPEG, nhưng trong định nghĩa encode thì tôi lại dùng là "COMPRESSION_GROUP4" nó tương đương với "CCITT Fax 4" vì vậy nên sinh ra lỗi.
Để sử lý lỗi này tôi phải dùng đúng dạng nén của hình TIFF là JPEG như sau:
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);
param.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);
Như trong đoạn mã tôi đã thay thế dáng mén của hình tif từ "COMPRESSION_GROUP4" thành "COMPRESSION_JPEG_TTN2" để giải quyết vấn đề lỗi phát sinh.
Hi vọng bài viết hỗ trợ được cho bạn phần nào khi gặp lỗi này. Rất mong sự đóng góp ý kiến của các bạn để bài viết được hoàn thiện hơn.
No comments:
Post a Comment