Bitmap总结

Bitmap可以说是图像处理中最重要的类,可以实现图像的剪切,旋转,旋转操作,我正在做的就是一个图片处理app,下面我会说一些在项目中遇到的问题和一些解决方案。
Android中Bitmap,byte[],Drawable相互转化
1,Drawable转bitmap:

Resources res = getResources(); 
    Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);

2,Bitmap → byte[]:

public byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
         bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
         return baos.toByteArray();
     }

3,byte[] → Bitmap:

byte[] b;
BitmapFactory.decodeByteArray(b, 0, b.length);

Bitmap平移,旋转,缩放, 翻转等操作
1 平移:
平移有2种方式:
(1)通过Imageview与Matrix:

Matrix matrix=new Matrix();
    int width=bitmap.getWidth();
    int height=bitmap.getHeight();
    matrix.postTranslate(width, height);
    mImageView.setImageMatrix(matrix);

(2)通过Matrix与bitmap:

Matrix matrix=mImageView.getImageMatrix();
    int width=bitmap.getWidth();
    int height=bitmap.getHeight();
    matrix.postTranslate(width, height);
    mNewBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
    mImageView.setImageBitmap(mNewBitmap);

2 旋转:
可以通过matrix.postRotate(rotate, x, y);, 可见Matrix 矩阵的强大,我当时看到这个的时候感受到了数学对于计算机的影响和重要性,第一个参数rotate是旋转的角度,后俩个参数是坐标,围绕哪个坐标来旋转。下面是一个把bitmap旋转任意角度的方法:

 public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }

3 缩放:
进行bitmap的缩放时一定要注意,要进行等比例缩放,如果缩放之后图片变形了就没有了它的意义,下面是我的缩放图片的方法,保持长宽比缩小bitmap:

public static Bitmap resizeBitmap(Bitmap bitmap, int maxWidth, int maxHeight) {
        int originWidth = bitmap.getWidth();
        int originHeight = bitmap.getHeight();

        // no need to resize
        if (originWidth < maxWidth && originHeight < maxHeight) {
            return bitmap;
        }

        int width = originWidth;
        int height = originHeight;

        // 若图片过宽, 则保持长宽比缩放图片
        if (originWidth > maxWidth) {
            width = maxWidth;

            double i = originWidth * 1.0 / maxWidth;
            height = (int) Math.floor(originHeight / i);

            bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
        }

        // 若图片过长, 则从上端截取
        if (height > maxHeight) {
            height = maxHeight;
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
        }

        return bitmap;
    }

4 翻转:
有的时候项目需求要求垂直翻转和水平翻转, sourceBitmap为你要旋转的bitmap

(1)垂直翻转:

 Matrix m = new Matrix();
m.postScale(1, -1);   //镜像垂直翻转
Bitmap newBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), m, true);

(2)水平翻转:

Matrix m = new Matrix();
m.postScale(-1, 1);   //镜像水平翻转
Bitmap newBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), m, true);

通过bitmap,ColorMatrix改变图片的 亮度,对比度, 饱和度,其中 AppConst.cropperImg为原图

1 改变亮度:

Bitmap brightness_bmp = Bitmap.createBitmap(AppConst.cropperImg.getWidth(), AppConst.cropperImg.getHeight(),
                        Bitmap.Config.ARGB_8888);
                int brightness = 127 - progress;
                ColorMatrix brightness_cMatrix = new ColorMatrix();
                brightness_cMatrix.set(new float[]{1, 0, 0, 0, brightness, 0, 1,
                        0, 0, brightness,// 改变亮度
                        0, 0, 1, 0, brightness, 0, 0, 0, 1, 0});

                Paint brightness_paint = new Paint();
                Canvas brightness_canvas = new Canvas(brightness_bmp);
                brightness_paint.setColorFilter(new ColorMatrixColorFilter(brightness_cMatrix));

                // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了
                brightness_canvas.drawBitmap(AppConst.cropperImg, 0, 0, brightness_paint);
// 将最终图片设置到控件上            
beautify_gpuimage.setImage(brightness_bmp);

2 曝光度:

//曝光度
                Bitmap contrast_bmp = Bitmap.createBitmap(AppConst.cropperImg.getWidth(), AppConst.cropperImg.getHeight(),
                        Bitmap.Config.ARGB_8888);
                // int brightness = progress - 127;
                float contrast = (float) ((progress + 64) / 128.0);
                ColorMatrix contrast_cMatrix = new ColorMatrix();
                contrast_cMatrix.set(new float[]{contrast, 0, 0, 0, 0, 0,
                        contrast, 0, 0, 0,// 改变对比度
                        0, 0, contrast, 0, 0, 0, 0, 0, 1, 0});

                Paint contrast_paint = new Paint();
                contrast_paint.setColorFilter(new ColorMatrixColorFilter(contrast_cMatrix));

                Canvas contrast_canvas = new Canvas(contrast_bmp);
                // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了
                contrast_canvas.drawBitmap(AppConst.cropperImg, 0, 0, contrast_paint);

                beautify_gpuimage.setImage(contrast_bmp);

3, 对比度:

//对比度
                Bitmap bmp = Bitmap.createBitmap(AppConst.cropperImg.getWidth(), AppConst.cropperImg.getHeight(),
                        Bitmap.Config.ARGB_8888);
                ColorMatrix cMatrix = new ColorMatrix();
                // 设置饱和度
                cMatrix.setSaturation((float) (progress / 100.0));

                Paint paint = new Paint();
                paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));

                Canvas canvas = new Canvas(bmp);
                // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了
                canvas.drawBitmap(AppConst.cropperImg, 0, 0, paint);

                beautify_gpuimage.setImage(bmp);

以上都是在做项目的过程中用到的对图片操作的总结,基本通过Bitmap都可以做到

你可能感兴趣的:(bitmap)