调用相机拍照,解决拍照时候内存溢出溢出的问题
废话少说,上代码
1 private static int CARMER_REQUEST_CODE = 1; 2 private String filedirName = "sencondIDCard"; 3 private String picFileName = ""; 4 private File picFile = null; 5 Uri imgUri = null; 6 OcrEngine oe; 7 IDCard ic; 8 9 private void startCarmer() { 10 11 File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "sencondIDCard"); 12 if(!dir.exists()){ 13 dir.mkdirs(); 14 } 15 16 Date date = null; 17 SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");// 获取当前时间,进一步转化为字符串 18 date = new Date(); 19 picFileName = format.format(date) + ".jpg"; 20 picFile = new File(dir,picFileName); 21 Uri u=Uri.fromFile(picFile); 22 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 23 24 camera.putExtra(MediaStore.Images.Media.ORIENTATION, 0); 25 camera.putExtra(MediaStore.EXTRA_OUTPUT, u); 26 27 startActivityForResult(camera, CARMER_REQUEST_CODE); 28 29 } 30 31 /** 32 * @param 将图片内容解析成字节数组 33 * @param inStream 34 * @return byte[] 35 * @throws Exception 36 */ 37 public byte[] readStream(InputStream inStream) throws Exception { 38 byte[] buffer = new byte[1024]; 39 int len = -1; 40 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 41 while ((len = inStream.read(buffer)) != -1) { 42 outStream.write(buffer, 0, len); 43 } 44 byte[] data = outStream.toByteArray(); 45 outStream.close(); 46 inStream.close(); 47 return data; 48 49 } 50 51 52 @Override 53 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 54 super.onActivityResult(requestCode, resultCode, data); 55 56 if (requestCode == CARMER_REQUEST_CODE ) { 57 try { 58 imgUri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), picFile.getAbsolutePath(), null, null)); 59 //u就是拍摄获得的原始图片的uri,剩下的你想干神马坏事请便…… 60 ContentResolver cr = getContentResolver(); 61 InputStream imgIS = imgIS = cr.openInputStream(imgUri); 62 // ic = oe.recognize(MainActivity.this, picFile.getAbsolutePath()); 63 ic = oe.recognize(MainActivity.this, readStream(imgIS)); 64 String cn = ic.getCardNo(); 65 String bt = ic.getBirth(); 66 if(cn != null){ 67 System.out.println("cn--->" + cn); 68 }else{ 69 System.out.println("cn---> cn is null" ); 70 } 71 System.out.println("--->" ); 72 } catch ( Exception e) { 73 e.printStackTrace(); 74 } 75 76 } 77 }
因僧问我西来意,我话居山不记年。
草履只栽三个耳,麻衣曾补两番肩。
东庵每见西庵雪,下涧长流上涧泉。
半夜白云消散后,一轮明月到床前。