简体   繁体   中英

Converting Bitmap to TIFF in Android

I am working on a project that has an external API that I need to submit an image through, and the requirement of that API is that the image be a TIFF with Group 4 Compression. I have tried several different approaches to convert my Bitmap to a TIFF including using JAI imageio in Android, porting libtiff into NDK and using OpenCV. JAI imageio is commonly recommend on here as a possible solution, but I found that even when I found a standalone version of JAI imageio that I still had an unmet dependency on javax.imageio and as a result could not build my app. Porting libtiff should work, and I have a branch in my code where I have begun this process, but due to the time required to do this I would prefer an alternate solution. OpenCV I also started to implement, but I decided the requirement to install a secondary OpenCV Manager APK in order to use it is not acceptable for my app, in addition to this being significant overkill for solving a simple problem.

Currently I have been working with the Android port of ImageMagick found here https://github.com/paulasiimwe/Android-ImageMagick and this appears to be a promising approach. So far I have set it up in my app and am able to output the image to a file in order to verify the image. I am able to use it to convert to a couple different image formats, but when I try to convert to a TIFF I receive a SIGSEGV and the app crashes.

The relevant code snippet is:

MagickImage image = MagickBitmap.fromBitmap(sizedImage);
image.setMagick("TIFF");
image.setImageFormat("TIFF");
image.setDepth(1);
image.setCompression(CompressionType.Group4Compression);

ImageInfo info = new ImageInfo();
info.setMagick("TIFF");
info.setCompression(CompressionType.Group4Compression);

image.writeImage(info);

I have tried various permutations of the different calls to setMagick and setCompression, all of which yield the same result. If a call to setMagick("TIFF") is made, then when it reaches image.writeImage(info) the app will crash with the following output:

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 2283

Has anyone used ImageMagick in Android to convert to a TIFF, or does anyone know of a clean method of converting to a TIFF in Android that is verified to actually work. I know that it must be possible because there are apps on the Play Store for converting image formats which include converting to TIFF, including one from Paul Asiimwe, who published this Android port.

EDIT #1:

By using the following code I am able to get an uncompressed TIFF that appears to be correct in all other regards, but it just will not compress it to Group 4 compression.

MagickImage image = MagickBitmap.fromBitmap(sizedImage);
image.setDepth(1);
image.setNumberColors(2);
ImageInfo info = new ImageInfo(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/output.tiff");
image.setCompression(CompressionType.Group4Compression);
image.writeImage(info);

info.setColorspace(ColorspaceType.GRAYColorspace);
info.setMonochrome(1);
info.setCompression(CompressionType.Group4Compression);
image.writeImage(info);

byte[] blob = image.imageToBlob(info);

我认为在这种情况下,您将必须实现自己的手动tiff转换。

You can use my library. It can save bitmap as tiff image https://github.com/Beyka/Android-TiffBitmapFactory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM