【Android】Android缩放图片大小,保存图片


public static Bitmap rotateBitmap(Bitmap bmp, float degree) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);  //旋转
        matrix.postScale(-1, 1);  //镜像水平翻转
        return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    }

Button button_snap = (Button)findViewById(R.id.buttonsnap);
        button_snap.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Camera.Size size = mCamera.getParameters().getPreviewSize();
                final int w = size.width;
                final int h = size.height;
                final YuvImage image = new YuvImage(mDataSingle, ImageFormat.NV21, w, h, null);
                ByteArrayOutputStream os = new ByteArrayOutputStream(mDataSingle.length);
                if( !image.compressToJpeg(new Rect(0,0,w,h), 100, os)){
                    return;
                }
                byte[] tmp = os.toByteArray();
                Bitmap bitmap = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
                Bitmap input = rotateBitmap(bitmap, -90);
                Bitmap bmp = input.copy(Bitmap.Config.ARGB_8888, true);
                saveBitmap(bmp, "size0.jpg");
                
                int nw = bmp.getWidth();
                int nh = bmp.getHeight();
                int[] pix = new int[nw * nh];
                bmp.getPixels(pix, 0, nw, 0, 0, nw, nh);
                int[] resultPixels = mFaceRecognizer.bitmapFace(pix, nw, nh); //返回结果
                Bitmap result = Bitmap.createBitmap(nw, nh, Bitmap.Config.ARGB_8888);
                result.setPixels(resultPixels,0,nw,0,0,nw,nh);
                imageSnapFace.setImageBitmap(result);
                saveBitmap(result, "result.jpg");
            }
        });


public static Bitmap getScaledBitmap(Bitmap m_img, float sx, float sy) {
        Matrix matrix = new Matrix();
        matrix.postScale(sx, sy);
        Bitmap rst = Bitmap.createBitmap(m_img, 0, 0, m_img.getWidth(), m_img.getHeight(), matrix, true);
        return rst;
    }
img = getScaledBitmap(imgTemp, 0.1f, 0.1f);   //缩小图片
imageView.setImageBitmap(img);


//保存图片
    public static void saveBitmap(Bitmap bm, String picName) {
        File f = new File("/storage/emulated/0/Pictures/", picName);
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
saveBitmap(img, "MyScaleImg.png");  //保存图片



完整版:

//缩放图片大小
    public static Bitmap getScaledBitmap(Bitmap m_img, float sx, float sy) {
        Matrix matrix = new Matrix();
        matrix.postScale(sx, sy);
        Bitmap rst = Bitmap.createBitmap(m_img, 0, 0, m_img.getWidth(), m_img.getHeight(), matrix, true);
        return rst;
    }

    //保存图片
    public static void saveBitmap(Bitmap bm, String picName) {
        File f = new File("/storage/emulated/0/Pictures/", picName);
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        detecter.release(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if (requestCode == PICTURE_CHOOSE) {
            if (intent != null) {
                try {
                    //加载图片并显示
                    ContentResolver resolver = getContentResolver();
                    Uri originalUri = intent.getData();//获得图片的uri
                    Bitmap imgTemp = MediaStore.Images.Media.getBitmap(resolver, originalUri);
                    img = getScaledBitmap(imgTemp, 0.3f, 0.3f);   //-----------缩小图片
                    imageView.setImageBitmap(img);

                    //获取图片的路径
                    Cursor cursor = getContentResolver().query(intent.getData(), null, null, null, null);
                    cursor.moveToFirst();
                    int idx = cursor.getColumnIndex(ImageColumns.DATA);
                    String fileSrc = cursor.getString(idx);
                    textViewDebug.setText("Picture:" + fileSrc);

                    textView.setText("图片加载成功!");
                    buttonDetect.setVisibility(View.VISIBLE);

                    saveBitmap(img, "MyScaleImg.png");  //---------------------保存图片
                    }catch (IOException e) {
                    textView.setText("TAG-->Error" + e.toString());
                    }
            }
            else {
                textViewDebug.setText("intent == null");
            }
        }
    }//onActivityResult()


//保存图片
    public static void saveBitmap(Bitmap bm, String picName) {
        File f = new File("/storage/emulated/0/Pictures/", picName);
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //length用户要求产生字符串的长度
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值