zoukankan      html  css  js  c++  java
  • Android-----图片处理工具

    直接上代码:

    public class BitmapTool {
    
    
        /**【给图片添加固定水印】**/
        public static Bitmap addWatermark(Bitmap bitmap , Context context){
            int width = bitmap.getWidth();      //
            int height = bitmap.getHeight();    //
            Bitmap icon = Bitmap.createBitmap(width , height , Bitmap.Config.ARGB_8888);//建立一个空的Bitmap
    
            Canvas canvas = new Canvas(icon);   //初始化画布到绘制的图像icon上
            Paint paint = new Paint();  //建立画笔
            paint.setDither(true);      //获取清晰的图像采样
            paint.setFilterBitmap(true);//过滤一些
            Rect src = new Rect(0 , 0 , width , height);//创建一个指定的新矩形坐标
            Rect dst = new Rect(0 , 0 , width , height);//创建一个指定的新矩形坐标
            canvas.drawBitmap(bitmap , src , dst , paint);  //将bitmap缩小或者扩大到 dst使用的填充区paint
            Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
            textPaint.setTextSize(50.0f);   //画笔字体大小
            textPaint.setTypeface(Typeface.DEFAULT);//采用默认的宽度DEFAULT_BOLD
            textPaint.setColor(Color.RED);  //采用的颜色
    
            Resources resources = context.getResources();
            Bitmap watermark = BitmapFactory.decodeResource(resources, R.drawable.mark);
            Bitmap mark = changeBitmapSize(watermark);
            String markText = "YouTu";
            canvas.drawBitmap(mark , width - mark.getWidth()*2 , height - mark.getHeight() , null);//绘制图标
            canvas.drawText(markText , width - mark.getWidth() ,height - mark.getHeight()/2 , textPaint);//绘制文字
    
            canvas.save(Canvas.ALL_SAVE_FLAG);  //保存
            canvas.restore();   //储存
            return icon;
        }
    
        /**【调整bitmap的尺寸大小】**/
        public static Bitmap changeBitmapSize(Bitmap bitmap){
            float markWidth = bitmap.getWidth() / 100;   //图标缩放比例
            float markHeight = bitmap.getHeight() / 100; //图标缩放比例
            Matrix matrix = new Matrix();       // 取得想要缩放的matrix参数
            matrix.postScale(markWidth , markHeight);
            Bitmap bitMap = Bitmap.createBitmap(bitmap , 0 , 0 ,bitmap.getWidth() ,bitmap.getHeight() ,matrix ,true);
            return bitMap;
        }
    
        /**【根据图片的Uri获取图片的绝对路径】**/
        public static String getReadPathFromUri(Context context , Uri uri){
            if (context == null || uri == null){
                return null;
            }
            if (uri.getScheme().equalsIgnoreCase("file")){
                /**
                 * 针对图片URI格式为Uri:: file:///storage/emulated/0/DCIM/Camera/IMG_20170613_132837.jpg
                 **/
                String uriToString = uri.toString();
                String filePath = uriToString.substring(uriToString.indexOf(":")+3);
                return filePath;
            }else if (uri.getScheme().equalsIgnoreCase("content")){
                /**
                 * 适配api11-api18,根据uri获取图片的绝对路径。
                 * 针对图片URI格式为Uri:: content://media/external/images/media/1028
                 **/
                String filePath = "";
                String [] projection = {MediaStore.Images.Media.DATA};
                CursorLoader loader = new CursorLoader(context , uri , projection ,null ,null , null);
                Cursor cursor = loader.loadInBackground();
                if (cursor != null && cursor.moveToFirst()){
                    filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
                    Log.e("filePath","filePath的值为"+filePath);
                    cursor.close();
                    if (filePath!=null){
                        return filePath;
                    }else{
                        return getReadFromUri_AboveAPI19(context,uri);
                    }
                }else{
                    Log.e("cursor","cursor的值为null");
                    return filePath;
                }
            }else{
                return getReadFromUri_AboveAPI19(context,uri);
            }
        }
    
        /**
         * 适配api19以上,根据uri获取图片的绝对路径
         */
        @SuppressLint("NewApi")
        private static String getReadFromUri_AboveAPI19(Context context , Uri uri){
            String filePath = null;
            String wholeID = null;
    
            wholeID = DocumentsContract.getDocumentId(uri);
    
            // 使用':'分割
            String id = wholeID.split(":")[1];
    
            String[] projection = { MediaStore.Images.Media.DATA };
            String selection = MediaStore.Images.Media._ID + "=?";
            String[] selectionArgs = { id };
    
            Cursor cursor = context.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
                    selection, selectionArgs, null);
            int columnIndex = cursor.getColumnIndex(projection[0]);
    
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(columnIndex);
            }
            cursor.close();
            return filePath;
        }
    
    
        /**【图片旋转,每次旋转度数:rotate】**/
        public static Bitmap bitmapRotate(Bitmap bitmap , int rotate){
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);   //旋转
            bitmap = Bitmap.createBitmap(bitmap,bitmap.getWidth()/10,0,bitmap.getHeight()*8/10,
                    bitmap.getHeight(),matrix,true);//重新绘制bitmap
            return bitmap;
        }
    
        /**【图片怀旧效果】**/
        public static Bitmap oldRemeber(Bitmap bitmap){
            int width = bitmap.getWidth();  //
            int height = bitmap.getHeight();//
            Bitmap map = Bitmap.createBitmap(width , height , Bitmap.Config.RGB_565);
            int pixColor = 0;
            int pixR = 0;
            int pixG = 0;
            int pixB = 0;
            int newR = 0;
            int newG = 0;
            int newB = 0;
            int [] pixels = new int[width * height];
            bitmap.getPixels(pixels,0,width,0,0,width,height);
            for (int i=0;i<height;i++){
                for (int k=0;k<width;k++){
                    pixColor = pixels[width * i + k];
                    pixR = Color.red(pixColor);
                    pixG = Color.red(pixColor);
                    pixB = Color.red(pixColor);
                    newR = (int)(0.393 * pixR + 0.769 * pixG + 0.189 * pixB);
                    newG = (int)(0.349 * pixR + 0.686 * pixG + 0.168 * pixB);
                    newB = (int)(0.272 * pixR + 0.534 * pixG + 0.131 * pixB);
                    int newColor = Color.argb(255,newR > 255 ? 255 : newR ,newG > 255 ? 255 : newG ,newB > 255 ? 255 :newB);
                    pixels[width * i + k ] = newColor;
                }
            }
            map.setPixels(pixels,0,width,0,0,width,height);
            return map;
        }
    
    
        /**【图片模糊效果】**/
        public static Bitmap vagueBitmap(Bitmap bmp){
            // 高斯矩阵
            int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
    
            int width = bmp.getWidth();
            int height = bmp.getHeight();
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    
            int pixR = 0;
            int pixG = 0;
            int pixB = 0;
    
            int pixColor = 0;
    
            int newR = 0;
            int newG = 0;
            int newB = 0;
    
            int delta = 16; // 值越小图片会越亮,越大则越暗
    
            int idx = 0;
            int[] pixels = new int[width * height];
            bmp.getPixels(pixels, 0, width, 0, 0, width, height);
            for (int i = 1, length = height - 1; i < length; i++)
            {
                for (int k = 1, len = width - 1; k < len; k++)
                {
                    idx = 0;
                    for (int m = -1; m <= 1; m++)
                    {
                        for (int n = -1; n <= 1; n++)
                        {
                            pixColor = pixels[(i + m) * width + k + n];
                            pixR = Color.red(pixColor);
                            pixG = Color.green(pixColor);
                            pixB = Color.blue(pixColor);
    
                            newR = newR + (int) (pixR * gauss[idx]);
                            newG = newG + (int) (pixG * gauss[idx]);
                            newB = newB + (int) (pixB * gauss[idx]);
                            idx++;
                        }
                    }
    
                    newR /= delta;
                    newG /= delta;
                    newB /= delta;
    
                    newR = Math.min(255, Math.max(0, newR));
                    newG = Math.min(255, Math.max(0, newG));
                    newB = Math.min(255, Math.max(0, newB));
    
                    pixels[i * width + k] = Color.argb(255, newR, newG, newB);
    
                    newR = 0;
                    newG = 0;
                    newB = 0;
                }
            }
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        }
    }
  • 相关阅读:
    20201107
    20201024
    20201020
    20200331
    20200330
    20200320
    20200319
    20200310
    20200221
    20190926
  • 原文地址:https://www.cnblogs.com/xiobai/p/12627736.html
Copyright © 2011-2022 走看看