您的位置:华清远见教育科技集团 >> Android资料 >> Android图像解码的实现  
 
Android图像解码的实现
分享到:

在智能机中,图像处理多通过专有DSP进行相关的编解码。为了显示图像,需要对图像进行解码,在智能终端中,具体的解码过程通常都需要借助aDSP来实现。下图所示为图像的解码过程。

图像解码过程
图像解码过程

在Android中,图像的解码均是基于ImageDecoder基类的,为了进行解码,需要通过BitmapFactory:: decodeStream()方法对输入流进行处理。下面是BitmapFactory:: decodeStream()方法的实现:

代码5-10 BitmapFactory:: decodeStream()的实现

public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts)
    {
      if (is==null) {
        return null;
      }
      if (!is.markSupported()) {
        is=new BufferedInputStream(is, 16 * 1024);//创建一个缓冲
      }
      is.mark(1024);
      Bitmap bm;
      if (is instanceof AssetManager.AssetInputStream) {
        return null;
      } else {
        try {
          bm=new Bitmap(is); //进行解码
      } catch (IOException e) {
          return null;
        }
      }
      return finishDecode(bm, outPadding, opts);
    }
    private static Bitmap finishDecode(Bitmap bm, Rect outPadding, Options opts) {
      if (bm==null || opts==null) {
        return bm;
      }
      final int density=opts.inDensity;
      if (density==0) {
        return bm;
      }
      bm.setDensity(density); //设置密度
      final int targetDensity=opts.inTargetDensity;
      if (targetDensity==0 || density==targetDensity
          || density==opts.inScreenDensity) {
        return bm;
      }
      byte[] np=bm.getNinePatchChunk();
      final boolean isNinePatch=false; //np != null && NinePatch.isNinePatchChunk(np);
      if (opts.inScaled || isNinePatch) {
        float scale=targetDensity / (float)density; //伸缩因子
        final Bitmap oldBitmap=bm;
        bm=Bitmap.createScaledBitmap(oldBitmap, (int) (bm.getWidth() * scale + 0.5f),(int) (bm.getHeight() * scale + 0.5f), true);//创建伸缩图像
        oldBitmap.recycle();
        if (isNinePatch) {
          bm.setNinePatchChunk(np);
        }
        bm.setDensity(targetDensity); //设置密度
      }
      return bm;
    }

终的解码工作则是通过SGL引擎来进行的,关于SGL引擎的更多内容,可以参考之前的免费资Android Skia 渲染概述Android Skia图形渲染等内容。

 更多相关文章

·Android Camera 参数的设置
·Android Camera的HAL接口
·Android中Camera数据的处理
·Android Camera拍照源码分析
·Android Camera原生服务架构