android实现拍照并转换为base64

来源:互联网 发布:淘宝手机店铺装修素材 编辑:程序博客网 时间:2024/06/05 10:22
gradule module文件添加:


dependencies {    compile 'com.mindorks:paracamera:0.2.2'}




manifest文件添加:


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.CAMERA" />




java实现代码:
 mimageIC = (ImageView)findViewById(R.id.imageView1);        camera = new Camera.Builder()                .resetToCorrectOrientation(true)// it will rotate the camera bitmap to the correct orientation from meta data                .setTakePhotoRequestCode(1)                .setDirectory("pics")                .setName("singpools_" + System.currentTimeMillis())                .setImageFormat(Camera.IMAGE_JPEG)                .setCompression(75)                .setImageHeight(300)                .build(this);        Button photoButton = (Button) this.findViewById(R.id.btnScanIC);        photoButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                try {                    camera.takePicture();                }catch (Exception e){                    e.printStackTrace();                }            }        });...   // For image// Get the bitmap and image path onActivityResult of an activity or fragment    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if(requestCode == Camera.REQUEST_TAKE_PHOTO){            Bitmap bitmap = camera.getCameraBitmap();            if(bitmap != null) {                mimageIC.setImageBitmap(bitmap);            }else{                Toast.makeText(this.getApplicationContext(),"Picture not taken!",Toast.LENGTH_SHORT).show();            }        }    }...


imageview取出图片转换为base64格式


   public String getImgBase64(ImageView imageView){        BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();        Bitmap bitmap = drawable.getBitmap();        ByteArrayOutputStream bos = new ByteArrayOutputStream();        bitmap.compress(Bitmap.CompressFormat.PNG,100,bos);        byte[] bb = bos.toByteArray();        String image = Base64.encodeToString(bb, Base64.NO_WRAP);        return image;    }...





注:如果后退时遇到一直loading的情况,打开这个activity之前,把下面这行代码注释掉
 //intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);



阅读全文
0 0