android图片操作

本文介绍了一系列实用的图片处理方法,包括图片尺寸调整、压缩、圆角处理及格式转换等关键技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、图片重画

/**
     * 将图片以指定尺寸重画
     *
     * @param map
     * @param height
     * @param width
     * @return
     */
    public Bitmap chagedImageSize(Bitmap map, int height, int width)
    {
        int imageWidth = map.getWidth();
        int imageHeight = map.getHeight();

        float proWidth = width / (float) imageWidth;
        float proHeight = height / (float) imageHeight;
        // 等比缩放
        if (proWidth > proHeight) {
            proWidth = proHeight;
        } else {
            proHeight = proWidth;
        }

        Matrix matrix = new Matrix();
        matrix.postScale(proWidth, proHeight);
        return Bitmap.createBitmap(map, 0, 0, imageWidth, imageHeight, matrix, true);
    }


2、从SD卡中获取图片并转换成Bitmap

public static Bitmap getBitmapFromSd(String path)
        {
            Bitmap bitmap=null;
            InputStream is=null;
            try
            {
                File file=new File(path);
                if(file.exists()&&file.isFile())
                {
                    is = StreamUtil.fileToInputStream(new File(path));
                    byte[] result=StreamUtil.readInputStream(is);
                    bitmap= BitmapFactory.decodeByteArray(result, 0, result.length);
                    is.close();    
                }
                
            } catch (IOException e)
            {
                e.printStackTrace();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                if(null!=is)
                {
                    is=null;
                }
            }    
            return bitmap;
        }


3、Bitmap压缩

public static Bitmap compressImage(Bitmap image)
        {

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
            int options = 100;
            while (baos.toByteArray().length / 1024 > 15)
            { // 循环判断如果压缩后图片是否大于10kb,大于继续压缩
                baos.reset();// 重置baos即清空baos
                options -= 10;// 每次都减少10
                image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
            }
            ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
            Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
            return bitmap;
        }


4、图片转成string,或用于上传图片,接收端再转换

   public static String convertIconToString(Bitmap bitmap)
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
            bitmap.compress(CompressFormat.PNG, 100, baos);
            byte[] appicon = baos.toByteArray();// 转为byte数组
            return Base64.encodeToString(appicon, Base64.DEFAULT);
        }

5、网络下载图片转换


public static byte[] getImageByte(String path)
        {
            byte[] res=null;    
            URL url=null;
            InputStream inputStream=null;
            HttpURLConnection conn =null;
            try
            {
                url = new URL(path);
                conn= (HttpURLConnection)url.openConnection();
                conn.setDoInput(true);
                conn.connect();
                inputStream=conn.getInputStream();             
                res = StreamUtil.readInputStream(inputStream);
                inputStream.close();
                conn.disconnect();                
            } catch (MalformedURLException e)
            {            
                e.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            } catch (Exception e)
            {
                e.printStackTrace();
            } finally
            {
                conn=null;
                inputStream=null;
                url=null;
            }           
            return res;                   
        }
         
        public static void writeToSd(byte res[],String filePath)
        {
            InputStream is=null;
            try
            {
                is=new ByteArrayInputStream(res);
                StreamUtil.inputStreamToFile(is, new File(filePath));
                is.close();    
            }catch(Exception e)
            {
              e.printStackTrace();    
            }finally
            {
                if(null!=is)
                {
                    is=null;
                }
            }        
        }
        
        public static byte[] bitmapBytes(Bitmap bm)
        {     
        ByteArrayOutputStream baos = new ByteArrayOutputStream();      
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);    
        return baos.toByteArray();
        }
       
6、图片圆角处理

//图片圆角处理
        public static Bitmap toRoundCorner(Bitmap bitmap, int dpRound,Context context)
        {       
            int pxRound=dip2px(context, dpRound);
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap           
                    .getHeight(), Config.ARGB_8888);       
            Canvas canvas = new Canvas(output);        
            //final int color = 0xff424242;   
            //final int color=R.color.pt_goods_center_textcolor;
            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 = pxRound;       
            paint.setAntiAlias(true);        
            canvas.drawARGB(0, 0, 0, 0);        
            //paint.setColor(color);     
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);       
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));      
            canvas.drawBitmap(bitmap, rect, rect, paint);
            return output;    
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值