【Android】Android缩放图片大小,保存图片
来源:互联网 发布:linux进程占用cpu过高 编辑:程序博客网 时间:2024/06/05 05:37
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
- 【Android】Android缩放图片大小,保存图片
- Android缩放图片大小小结
- android图片大小按比例缩放
- Android:图片大小缩放drawable
- android上传图片压缩图片大小
- numpy缩放图片/调整图片大小
- Android 图片缩放
- Android 图片缩放-Matrix
- android图片的缩放
- android图片的缩放
- Android 图片缩放-Matrix
- android图片的缩放 .
- android 两点缩放图片
- android ImageView图片缩放
- android 图片缩放
- android 图片缩放
- android 两点缩放图片
- Android图片缩放方法
- lsd:tracking
- Spring-@Scheduled
- HTTP协议 (五) 代理
- 保护模式中断
- C#入门10.1——接口的声明
- 【Android】Android缩放图片大小,保存图片
- leetcode_c++:树:Maximum Depth of Binary Tree(104)
- ButterKnife 8.3.0 不生效问题
- 神州顺利办发的红包真的好使吗?
- FreeBSD下vsftpd配置
- 结构体
- 如何绘制caffe训练过程中的loss和accurary的曲线??
- Linux防火墙
- c语言:读取文件中的指定内容