调用Android相机拍照,解决了拍照时候内存溢出溢出的问题

发布时间:2016-12-11 8:25:13 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"调用Android相机拍照,解决了拍照时候内存溢出溢出的问题",主要涉及到调用Android相机拍照,解决了拍照时候内存溢出溢出的问题方面的内容,对于调用Android相机拍照,解决了拍照时候内存溢出溢出的问题感兴趣的同学可以参考一下。

在别处上看到的文章,解决了我在开发中的拍照后内存溢出的问题,下面就是代码 private static int CARMER_REQUEST_CODE = 1; private String filedirName = "sencondIDCard"; private String picFileName = ""; private File picFile = null; Uri imgUri = null; OcrEngine oe; IDCard ic; private void startCarmer() { File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "sencondIDCard"); if(!dir.exists()){ dir.mkdirs(); } Date date = null; SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");// 获取当前时间,进一步转化为字符串 date = new Date(); picFileName = format.format(date) + ".jpg"; picFile = new File(dir,picFileName); Uri u=Uri.fromFile(picFile); Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); camera.putExtra(MediaStore.Images.Media.ORIENTATION, 0); camera.putExtra(MediaStore.EXTRA_OUTPUT, u); startActivityForResult(camera, CARMER_REQUEST_CODE); } /** * @param 将图片内容解析成字节数组 * @param inStream * @return byte[] * @throws Exception */ public byte[] readStream(InputStream inStream) throws Exception { byte[] buffer = new byte[1024]; int len = -1; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CARMER_REQUEST_CODE ) { try { imgUri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), picFile.getAbsolutePath(), null, null)); //u就是拍摄获得的原始图片的uri,剩下的你想干神马坏事请便…… ContentResolver cr = getContentResolver(); InputStream imgIS = imgIS = cr.openInputStream(imgUri); // ic = oe.recognize(MainActivity.this, picFile.getAbsolutePath()); ic = oe.recognize(MainActivity.this, readStream(imgIS)); String cn = ic.getCardNo(); String bt = ic.getBirth(); if(cn != null){ System.out.println("cn--->" + cn); }else{ System.out.println("cn---> cn is null" ); } System.out.println("--->" ); } catch ( Exception e) { e.printStackTrace(); } } }

上一篇:Android Developers:构建一个灵活的UI
下一篇:MTK6575 量产软件修改信息

相关文章

相关评论