android Bitmap图片的相关处理操作

android Bitmap图片的相关处理操作

/**
     * 从网络获取图片并返回 Bitmap 对象
     * @param urlpath 网络URL
     * @return
     */
    public static Bitmap getImageFromUrl(String urlpath){
        InputStream inputStream = null;
        Bitmap bitmap = null;
        try {
            URL url = new URL(urlpath);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
            conn.setRequestMethod("GET");   //设置请求方法为GET  
            conn.setReadTimeout(5*1000);    //设置请求过时时间为5秒  
            inputStream = conn.getInputStream();   //通过输入流获得图片数据  
            byte[] buffer = new byte[1024];  
            int len = 0;  
            ByteArrayOutputStream bos = new ByteArrayOutputStream();  
            while((len = inputStream.read(buffer)) != -1) {  
                bos.write(buffer, 0, len);  
            }  
            bos.close();
            byte[] data = bos.toByteArray();//获得图片的二进制数据
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  //生成位图  
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bitmap;
    }
    
    /**
     * 按正方形裁切图片
     */
    public static Bitmap ImageCrop(Bitmap bitmap) {
        int w = bitmap.getWidth(); // 得到图片的宽,高
        int h = bitmap.getHeight();

        int wh = w > h ? h : w;// 裁切后所取的正方形区域边长

        int retX = w > h ? (w - h) / 2 : 0;//基于原图,取正方形左上角x坐标
        int retY = w > h ? 0 : (h - w) / 2;

        //下面这句是关键
        return Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null, false);
    }
    
    //使用Bitmap加Matrix来缩放   
    public static Bitmap resizeImage(Bitmap bitmap, int w, int h)   
    {    
        int width = bitmap.getWidth();    
        int height = bitmap.getHeight();    
        int newWidth = w;    
        int newHeight = h;    

        float scaleWidth = ((float) newWidth) / width;    
        float scaleHeight = ((float) newHeight) / height;    

        Matrix matrix = new Matrix();    
        matrix.postScale(scaleWidth, scaleHeight);  
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,    
                        height, matrix, true);  
        if(!bitmap.isRecycled()){
            bitmap.recycle();
            bitmap = null;
        }
        return resizedBitmap;    
    }
    
    //使用Bitmap加Matrix来缩放   [path 图片路径]
    public static Bitmap resizeImage(String path, int w, int h)   
    {    
        //Bitmap bitmap = resizeImage(path);
        //Bitmap bitmap = BitmapFactory.decodeFile(path);
        Bitmap bitmap = decodeFile(path,w,h);
        int width = bitmap.getWidth();    
        int height = bitmap.getHeight();    
        int newWidth = w;    
        int newHeight = h;    
        float initScale=(float)0.99;
        float scaleWidth = ((float) newWidth) / width;    
        float scaleHeight = ((float) newHeight) / height;
        
        if(scaleWidth<1.0||scaleHeight<1.0){
            initScale = scaleHeight > scaleWidth ? scaleWidth : scaleHeight;
        }
        else{
            return bitmap;
        }
        Matrix matrix = new Matrix();    
        matrix.postScale(initScale, initScale);
        
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,height, matrix, true);    
        if(!bitmap.isRecycled()){
            bitmap.recycle();
            bitmap = null;
        }
        return resizedBitmap;    
    }
  
/*
     *图片圆角的设置 
     *bitmap 传入的bitmap 对象
     *pixels 自定义圆角 角度
     */    
    public  static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { 
        Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
        Canvas canvas = new Canvas(outputBitmap); 
        final int color = 0xff424242; 
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
        final RectF rectF = new RectF(rect); 
        final float roundPx = pixels; 
        
        paint.setAntiAlias(true); 
        canvas.drawARGB(0, 0, 0, 0); 
        paint.setColor(color); 
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        
        canvas.drawBitmap(bitmap, rect, rect, paint); 
        return outputBitmap; 
    } 

 

 

 

posted @ 2015-03-18 15:21  sangxb  阅读(346)  评论(0编辑  收藏  举报