调用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();
            } 

        }
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值