如何寫圖片壓縮框架

鏈?zhǔn)骄幊?/h4>
try {
    final InputStream is = getResources().getAssets().open("test-6.jpg");
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = mConfig;
    Bitmap originBitmap = BitmapFactory.decodeStream(is, null, options);
    //上面代碼就是通過(guò)流得到Bitmap對(duì)象
    KmoCompress.FileCompressOptions compressOptions = new KmoCompress.FileCompressOptions();//設(shè)置自己的配置對(duì)象
    compressOptions.config = mConfig;
    KmoCompress.getInstance().source(originBitmap).asFile().withOptions(compressOptions).compress(new FileWithBitmapCallback() {
        @Override
        public void callback(boolean isSuccess, Bitmap bitmap, String outfile, Throwable t) {
            if (!isSuccess) {
                Logger.e("error: " + t.getMessage());
                mCompressTv.setText("compress file failed!");
                return;
            }
            File file = new File(outfile);
            setupCompressInfo(bitmap, outfile, file.length());
        }
    });
    is.close();
} catch (Exception e) {
    e.printStackTrace();
}

看到KmoCompress.getInstance().source(originBitmap).asFile().XXX這段就是鏈?zhǔn)骄幊獭?/p>

首先由一個(gè)單例發(fā)出,單例里面有source這個(gè)方法

public synchronized CompressEngine source(Bitmap bitmap) {
    return new CompressEngine().source(bitmap);
}

這個(gè)方法很簡(jiǎn)單,就是new 一個(gè)對(duì)象調(diào)用該對(duì)象的source,這樣就把業(yè)務(wù)轉(zhuǎn)移到了CompressEngine這個(gè)對(duì)象身上。

public CompressEngine source(Bitmap bitmap) {
    mSourceType = SourceType.BITMAP;
    mSource = bitmap;
    return this;
}

這個(gè)對(duì)象的source方法就是存儲(chǔ)一些東西。

所以我們小結(jié)如下:

鏈?zhǔn)骄幊淘?號(hào)后面無(wú)非就是兩種情況:

  • new 對(duì)象然后將業(yè)務(wù)轉(zhuǎn)移到該對(duì)象身上。
  • this 就是當(dāng)前對(duì)象實(shí)現(xiàn)業(yè)務(wù)
/**
 * @return  文件處理引擎,里面包含了圖片源和圖片類型
 */
public FileCompressEngine asFile() {
    return CompressEngineFactory.buildFileCompressEngine(mSource, mSourceType);
}
/**
 * @param options 配置文件
 * @return
 */
public FileCompressEngine withOptions(KmoCompress.FileCompressOptions options) {
    options.config = CompressUtils.filterConfig(options.config);//對(duì)config轉(zhuǎn)化,如果是ARGB_8888,ARGB_4444,ALPHA_8都對(duì)應(yīng)成8888,RGB_565就是RGB_565
    mCompressOptions = options;//并將配置保存
    return this;
}

此時(shí)鏈?zhǔn)骄幊桃呀?jīng)傳遞到withOptions方法,他返回的是FileCompressEngine對(duì)象本身,那也就是說(shuō)后面的compress方法也在該對(duì)象內(nèi)部實(shí)現(xiàn)。

我們目前來(lái)看結(jié)果,上面代碼到withOptions都是處理參數(shù)的保存,真正的壓縮還沒(méi)有執(zhí)行。我們想一個(gè)問(wèn)題哈,就是壓縮是需要時(shí)間的,在Android里面耗時(shí)的操作不要 給主線程放,那么應(yīng)該怎么讓compress去通過(guò)我們傳遞進(jìn)來(lái)的數(shù)據(jù)源在子線程中壓縮數(shù)據(jù)呢?同時(shí)我們是需要返回結(jié)果的傳統(tǒng)的new Thread(){run()}.start();已經(jīng)不能滿足我們,因?yàn)槲覀円獙⒔Y(jié)果刷入主線程中,我們?cè)趺醋瞿兀?/p>

線程池+任務(wù)

根據(jù)上面所以這里就對(duì)應(yīng)一個(gè)問(wèn)題模型:數(shù)據(jù)源我們現(xiàn)在處理了,也傳遞到此時(shí)的處理類中,但是由于處理數(shù)據(jù)比較耗時(shí),而且我們需要處理結(jié)果在主線程中,怎么辦?

先看怎么寫:

  1. 用單例的方式生成一個(gè)線程池
public class CompressExecutor {

    private CompressExecutor() {
        throw new RuntimeException("can not be a instance");
    }

    private static final ThreadPoolExecutor DEFAULT_COMPRESS_EXECUTOR;

    static {
        int nThreads = Runtime.getRuntime().availableProcessors() + 1;
        DEFAULT_COMPRESS_EXECUTOR = new CompressThreadPool(
                nThreads,
                nThreads,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                new CompressThreadFactory()
        );
    }

    public static ThreadPoolExecutor getExecutor() {
        return DEFAULT_COMPRESS_EXECUTOR;
    }

}
  1. 繼承線程池

繼承的目的是:我們需要在線程任務(wù)執(zhí)行完成之后打印一些信息

public class CompressThreadPool extends ThreadPoolExecutor {

    public CompressThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public CompressThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public CompressThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public CompressThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    /**
     * Method invoked prior to executing the given Runnable in the given thread.
     *
     * @param t
     * @param r
     */
    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        //ignore...
    }

    /**
     * Method invoked upon completion of execution of the given Runnable.
     *
     * @param r
     * @param t
     */
    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        KmoCompressUtil.printExceptionMessage(t);
    }

    /**
     * Method invoked when the Executor has terminated.
     */
    @Override
    protected void terminated() {
        super.terminated();
        //ignore...
    }
}
public class FileCompressEngine extends CompressEngine {
    ...
    Bitmap bitmap = (Bitmap) mSource;
    CompressExecutor.getExecutor()
            .execute(new CompressFutureTask<CompressResult>(new FileCompressCallableTasks.BitmapAsFileCallable(mCompressOptions, shouldReturnBitmap, bitmap)
                    , new DefaultCallbackDispatcher<CompressResult>(callback)
            ));
    ...
}

我們觀察一下new CompressFutureTask的繼承結(jié)構(gòu):

public class CompressFutureTask<T> extends FutureTask<T> {

這下就和我們上一次的博客吻合,在線程任務(wù)執(zhí)行過(guò)程中,work包裝了我們的線程,最終調(diào)用了run方法,我們?cè)贔utureTask中看看,最終的run方法。

public void run() {
    if (state != NEW ||
        !U.compareAndSwapObject(this, RUNNER, null, Thread.currentThread()))
        return;
    try {
        Callable<V> c = callable;
        if (c != null && state == NEW) {
            V result;
            boolean ran;
            try {
                result = c.call();
                ran = true;
            } catch (Throwable ex) {
                result = null;
                ran = false;
                setException(ex);
            }
            if (ran)
                set(result);
        }
    } finally {
        runner = null;
        int s = state;
        if (s >= INTERRUPTING)
            handlePossibleCancellationInterrupt(s);
    }
}

看到是調(diào)用call和set方法。

所以我們的業(yè)務(wù)核心在call方法上

call方法是外界塞給FutureTask的對(duì)應(yīng)接口。

  public static final class FileAsFileCallable extends BaseFileCompressCallable {
        private File mFile;

        public FileAsFileCallable(KmoCompress.FileCompressOptions options, boolean withBitmap, File file) {
            super(options, withBitmap);
            mFile = file;
        }

        @Override
        public CompressResult call() throws Exception {
            CompressResult result = null;
            FileInputStream fis = null;
            try {
                if (mCompressOptions != null && mCompressOptions.overrideSource)
                    mCompressOptions.outfile = mFile.getAbsolutePath();
                fis = new FileInputStream(mFile);
                result = new FileCompressor().compress(CompressUtils.transformToByteArray(fis), mCompressOptions, shouldReturnBitmap, true);
            } finally {
                try {
                    if (fis != null)
                        fis.close();
                } catch (IOException e) {
                    //ignore...
                }
            }
            return result;
        }
    }

所以call是壓縮的核心那后面就不多說(shuō),主要是一些壓縮比例轉(zhuǎn)化等等問(wèn)題以后我們?cè)谡f(shuō)壓縮如何壓縮。這個(gè)就直接偏向應(yīng)用層,不需要架構(gòu)的概念,我們今天只討論架構(gòu)的東西。OK就到這了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,996評(píng)論 25 709
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評(píng)論 19 139
  • 一 潮水褪走了 風(fēng)還沒(méi)來(lái) 燈塔的余光淹沒(méi) 寄居蟹的腳印 水鳥??吭诟劭?成為唯一的證人 用一捧水 澆灌游魚的夢(mèng) 把...
    雨子1983閱讀 216評(píng)論 2 2
  • 一葉知秋落海堤,蕭瑟昏夕,渫雨凄迷。天開(kāi)陰?kù)V夢(mèng)依稀,鴻雁輕啼,望斷云低。 殘熱猶威汗染衣,茶飲神移...
    海1619閱讀 288評(píng)論 2 7
  • 《遇見(jiàn)一棵花樹》 ……………………………………………… 每次讀席慕容的詩(shī),那些柔軟清麗的句子,猶如微風(fēng)般輕輕拂來(lái)…...
    女俠K的解憂年畫鋪閱讀 612評(píng)論 0 0

友情鏈接更多精彩內(nèi)容