1、布局中显示图片
在布局的xml中布局图片的时候用ImageView,用src去指定图片所在位置。以下代码所示,指定的就是工程目录(/res/drawable)中文件名为unknown.png的图片。
这里要注意的是Android Studio在布局时只认png格式的图片,即便是jpeg格式,仅把后缀改成png也不行,编译时会不经过。android
<ImageView android:id="@+id/iv_mytest" android:src="@drawable/unknown" />
可是,也不等于jpeg格式的图片就不能显示,咱们能够经过以下代码处理的方式来展现到界面上。缓存
String imgPath = Environment.getExternalStorageDirectory() + "test.jpg"; ImageView iv_mytest = (ImageView) findViewById(R.id.iv_mytest); iv_mytest.setVisibility(View.VISIBLE); if(!imgPath.equals("")) { Bitmap tempBitmap = BitmapFactory.decodeFile(imgPath); iv_mytest.setImageBitmap(tempBitmap);//显示图片 }
2、拍照后显示图片
拍照流程为获取缓存图片路径->进入拍照界面->拍照界面拍照后自动存到缓存图片路径中->进入回调函数->对缓存图片进行处理(如旋转缩放等)并存储到本身指定位置->删除缓存路径图片。
具体代码以下所示:ide
private String tmpfilename = ""; //调用拍照界面 private void photograph(){ try { // 跳转至拍照界面 String sdStatus = Environment.getExternalStorageState(); if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用 ToastUtil.showToastMsgError(MyTestActivity.this,"SD card is not avaiable/writeable right now."); return; } tmpfilename=getTempFilePath(); File out = new File(tmpfilename); Intent intentPhote = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (Build.VERSION.SDK_INT >= 24) { //临时添加一个拍照权限 intentPhote.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //经过FileProvider获取uri contentUri = FileProvider.getUriForFile(getActivity(), "com.tmri.rfid.eri.internet.fileProvider", out); intentPhote.putExtra(MediaStore.EXTRA_OUTPUT, contentUri); } else { mImageCaptureUri = Uri.fromFile(out); intentPhote.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri); } startActivityForResult(intentPhote, 1); }catch (Exception e) { } } /** * 获取原缓存图片存储路径 * @return */ private String getTempFilePath() { // 照片全路径 String fileName = ""; // 文件夹路径 String pathUrl = Environment.getExternalStorageDirectory()+"/tmp/"; imagename = "mytest.png"; File file = new File(pathUrl); file.mkdirs();// 建立文件夹 fileName = pathUrl + imagename; return fileName; } //拍取照后的回调 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == Activity.RESULT_OK) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 设置了此属性必定要记得将值设置为false Bitmap bitmap = BitmapFactory.decodeFile(tmpfilename); // 防止OOM发生 options.inJustDecodeBounds = false; saveMyBitmap(imagename,bitmap); File old = new File(Environment.getExternalStorageDirectory() + "/tmp/"); FileUtil.deleteFile(old);//删除缓存图片 String resultMsg = "图片已保存"; ToastUtil.showToastMsgSuccess(this, resultMsg); } } //将图像保存到SD卡中 public void saveMyBitmap(String bitName, Bitmap mBitmap) { String thisDate = formatter_date.format(new Date()); File f = FileUtil.getFilePath(Environment.getExternalStorageDirectory() + "/rfid/prehairpin/"+thisDate+"/", bitName); String realfilepath = f.getPath(); FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (Exception e) { e.printStackTrace(); } Matrix matrix = new Matrix(); // 按照固定大小对图片进行缩放 matrix.postScale(0.3f, 0.3f); System.out.println(mBitmap.getWidth() + mBitmap.getHeight()); if (mBitmap.getHeight() < mBitmap.getWidth()) { matrix.postRotate(90); //翻转90度 } mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true); mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); try { fOut.write(("my secret message").getBytes());//我在这边偷偷给图片结尾藏了一些信息 fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } }
3、图片的处理
旋转、缩放等操做咱们是经过Matrix来处理的,Matrix还有不少其余图形处理的方法,能够另开一篇去讲述。函数
Matrix matrix = new Matrix(); // 按照固定大小对图片进行缩放 matrix.postScale(0.3f, 0.3f); if (mBitmap.getHeight() < mBitmap.getWidth()) { matrix.postRotate(90); //翻转90度 } mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true); mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
上面是按比例缩小,下面是按固定分辨率缩放布局
Matrix matrix = new Matrix(); int dstWidth = 800; int dstHeight = 600; if (mBitmap.getHeight() > mBitmap.getWidth()) { matrix.postRotate(90); //翻转90度 final float sx = dstWidth / (float) mBitmap.getHeight(); final float sy = dstHeight / (float) mBitmap.getWidth(); matrix.postScale(sx, sy); }else{ final float sx = dstWidth / (float) mBitmap.getWidth(); final float sy = dstHeight / (float) mBitmap.getHeight(); matrix.postScale(sx, sy); } Bitmap endBit = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true); endBit.compress(Bitmap.CompressFormat.JPEG, 100, fOut);//jpeg格式缩放的图片大小基本只占png的一半
保存图片,并将字符存入图片(有时候拍照后但愿将一些信息与图片绑定起来,直接记录文件名又担忧被人篡改,就想到了这种在图片文件末尾记录一些信息的方式)post
String thisDate = formatter_date.format(new Date()); File f = FileUtil.getFilePath(Environment.getExternalStorageDirectory() + "/rfid/prehairpin/"+thisDate+"/", bitName); String realfilepath = f.getPath(); FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (Exception e) { e.printStackTrace(); } //我在这边偷偷给图片结尾藏了一些信息 try { fOut.write(("my secret message").getBytes()); fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); }
4、图片格式的差别png、jpeg由于格式的差别,在内部添加字符信息时会不同,好比png格式结尾处就是图片信息,因此添加的话直接在末尾添加就能够;而jpeg不行,jpeg末尾是有固定格式信息的,直接加载末尾虽然不影响图片显示,可是在解析时就会由于位置偏移解析出来的字符信息就不对了。这一块内容还有待去深刻研究下,当时也只是试验了两种格式,发现了这一问题。
更新于20190711,经尝试jpeg也能够再末尾写入信息,且jpeg缩放的图片大小仅占png格式缩放的图片一半大小。
可是,此时jpeg格式的图片直接解析是没有问题的,一经上传就没法解析末尾写入的信息了。
为什么通过上传后末尾的信息没法解析的问题,还有待研究!还望知道的朋友不吝赐教!ui