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; 
    } 
相關文章
相關標籤/搜索