简体   繁体   English

在Android中将位图转换为TIFF

[英]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. 我正在一个具有外部API的项目上工作,我需要通过该API提交图像,并且该API的要求是该图像必须是具有第4组压缩的TIFF。 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. 我尝试了几种将位图转换为TIFF的方法,包括在Android中使用JAI imageio,将libtiff移植到NDK以及使用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. 通常在这里推荐使用JAI imageio作为可能的解决方案,但是我发现,即使我找到了JAI imageio的独立版本,我对javax.imageio的依赖仍然未得到满足,因此无法构建我的应用程序。 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. 移植libtiff应该可以工作,并且我的代码中已经有一个分支开始了该过程,但是由于需要时间,所以我希望使用替代解决方案。 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. 我也开始实施OpenCV,但我决定安装第二个OpenCV Manager APK以便使用它的要求对我的应用程序来说是不可接受的,此外,这对于解决一个简单的问题来说是过大的杀伤力。

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. 目前,我一直在使用ImageMagick的Android端口(位于https://github.com/paulasiimwe/Android-ImageMagick) ,这似乎是一种很有前途的方法。 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. 我可以使用它来转换为几种不同的图像格式,但是当我尝试转换为TIFF时,会收到SIGSEGV,并且应用程序崩溃。

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. 我尝试了对setMagick和setCompression的不同调用的各种排列,所有这些产生相同的结果。 If a call to setMagick("TIFF") is made, then when it reaches image.writeImage(info) the app will crash with the following output: 如果调用setMagick(“ TIFF”),则在到达image.writeImage(info)时,应用将崩溃,并显示以下输出:

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. 是否有人在Android中使用ImageMagick转换为TIFF,或者是否有人知道一种经验证可以实际工作的在Android中转换为TIFF的干净方法。 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. 我知道这是有可能的,因为Play商店中有一些应用程序可以转换图像格式,包括转换成TIFF,其中包括来自发布此Android端口的Paul Asiimwe的应用程序。

EDIT #1: 编辑#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. 通过使用以下代码,我可以获得在所有其他方面都正确的未压缩TIFF,但是它不会将其压缩为Group 4压缩。

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 可以将位图保存为tiff图像https://github.com/Beyka/Android-TiffBitmapFactory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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