六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 48|回复: 0

android画图------字节数组转化为图片

[复制链接]

升级  82%

292

主题

292

主题

292

主题

进士

Rank: 4

积分
910
 楼主| 发表于 2013-1-15 02:47:17 | 显示全部楼层 |阅读模式
在这个例程当中,主要讲了将一个图片压缩成字节流,然后转化成数组 在变成图片的故事。
首先 图片压缩成流 在转变成图片的方法还是值得学习的:
  private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
                                    int quality) {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            src.compress(format, quality, os);            

            byte[] array = os.toByteArray();
            return BitmapFactory.decodeByteArray(array, 0, array.length);
        }

初始化 图片:
mBitmaps = new Bitmap[6];
            // these three are initialized with colors[]
            mBitmaps[0] = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,
                                              Bitmap.Config.ARGB_8888);
            mBitmaps[1] = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,                                              Bitmap.Config.RGB_565);
            mBitmaps[2] = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,
                                              Bitmap.Config.ARGB_4444);
            
            // these three will have their colors set later
            mBitmaps[3] = Bitmap.createBitmap(WIDTH, HEIGHT,
                                              Bitmap.Config.ARGB_8888);
            mBitmaps[4] = Bitmap.createBitmap(WIDTH, HEIGHT,
                                              Bitmap.Config.RGB_565);
            mBitmaps[5] = Bitmap.createBitmap(WIDTH, HEIGHT,
                                              Bitmap.Config.ARGB_4444);
            for (int i = 3; i <= 5; i++) {
                mBitmaps.setPixels(colors, 0, STRIDE, 0, 0, WIDTH, HEIGHT);
            }
            
for (int i = 0; i < mBitmaps.length; i++) {
                mJPEG = codec(mBitmaps, Bitmap.CompressFormat.JPEG, 80);
                mPNG = codec(mBitmaps, Bitmap.CompressFormat.PNG, 0);

最后将图片绘画在屏幕上
  @Override protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.WHITE);

            for (int i = 0; i < mBitmaps.length; i++) {
                canvas.drawBitmap(mBitmaps, 0, 0, null);
                canvas.drawBitmap(mJPEG, 80, 0, null);
                canvas.drawBitmap(mPNG, 160, 0, null);
                canvas.translate(0, mBitmaps.getHeight());
            }
            
            // draw the color array directly, w/o craeting a bitmap object
            canvas.drawBitmap(mColors, 0, STRIDE, 0, 0, WIDTH, HEIGHT,
                              true, null);
            canvas.translate(0, HEIGHT);
            canvas.drawBitmap(mColors, 0, STRIDE, 0, 0, WIDTH, HEIGHT,
                              false, mPaint);
        }
上面的绘制 有三种情况 绘制jpeg 绘制png 直接绘制原数组。
mBitmaps.setPixels(colors, 0, STRIDE, 0, 0, WIDTH, HEIGHT);
也可以通过上面的方式来改变颜色
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表