android中PNG/JPEG 转换为TIFF

1.新建项目(可以添加C++support)并使用依赖

 compile 'com.github.beyka:androidtiffbitmapfactory:0.9.8.2'
复制代码

2.加入JNI文件

 地址: https://github.com/julienr/libpng-android
 as项目 new->Folder->JNI Folder
 将下载解压的jni文件拷贝到项目jni目录下
 并为项目配置本地的NDK路径
 添加android.useDeprecatedNdk=true到gradle.properties文件
复制代码

3.配置build.gradle文件

 android {
  sourceSets {
    main {
        jni.srcDirs = ['build/native-libs']
    }
}
  buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
复制代码

4.增加权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
复制代码

PS

 可以使用JPEG压缩方案
 地址: https://github.com/Suvitruf/libjpeg-version-9-android
复制代码

转换示例代码

public void transform() {
        File out = new File("/sdcard/image.tif");
        if (out.exists()) {
            out.delete();
        }
        //Open some image
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.jag1);
//Create options for saving
        TiffSaver.SaveOptions options = new TiffSaver.SaveOptions();
//By default compression mode is none
        options.compressionScheme = CompressionScheme.LZW;
//By default orientation is top left
        options.orientation = Orientation.LEFT_TOP;
//Add author tag to output file
        options.author = "beyka";
//Add copyright tag to output file
        options.copyright = "Some copyright";
//Save image as tif. If image saved succesfull true will be returned
        boolean saved = TiffSaver.saveBitmap("/sdcard/image.tif", bitmap, options);

        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.fire1);
        // 追加一页
        boolean append = TiffSaver.appendBitmap("/sdcard/image.tif", bitmap1,options);
        // 追加一页 与上一句等效
        TiffSaver.appendBitmap("/sdcard/image.tif",1, bitmap1);
    }
复制代码

显示TIFF示例代码

private void showTiff() {
        File file = new File("/sdcard/image.tif");
        if (!file.exists()) {
            return;
        }
//Read data about image to Options object
        TiffBitmapFactory.Options options = new TiffBitmapFactory.Options();
        options.inJustDecodeBounds = true;
        TiffBitmapFactory.decodeFile(file, options);

        int dirCount = options.outDirectoryCount;

//Read and process all images in file 分页取出
        for (int i = 0; i < dirCount; i++) {
            options.inDirectoryNumber = i;
            TiffBitmapFactory.decodeFile(file, options);
            int curDir = options.outCurDirectoryNumber;
            int width = options.outWidth;
            int height = options.outHeight;
            //Change sample size if width or height bigger than required width or height
            int inSampleSize = 1;
            if (height > 300 || width > 100) {

                final int halfHeight = height / 2;
                final int halfWidth = width / 2;

                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) > 300
                        && (halfWidth / inSampleSize) > 100) {
                    inSampleSize *= 2;
                }
            }
            options.inJustDecodeBounds = false;
            options.inSampleSize = inSampleSize;

            // Specify the amount of memory available for the final bitmap and temporary storage.
            options.inAvailableMemory = 20000000; // bytes

            Bitmap bmp = TiffBitmapFactory.decodeFile(file, options);
            if (i == 0) {
                show.setImageBitmap(bmp);
            } else if (i == 1) {
                show1.setImageBitmap(bmp);
            }
        }
    }
复制代码
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值