分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
通常用Base64这种编解码方式将二进制数据转换成可见的字符串格式,就是我们常说的大串,10块钱一串的那种,^_^。
Android的android.util包下直接提供了一个功能十分完备的Base64类供我们使用,下面就演示一下如何将一张图片进行Base64的编解码。
1.找到那张图片
public void onEncodeClicked(View view) { //select picture Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, OPEN_PHOTO_FOLDER_REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(OPEN_PHOTO_FOLDER_REQUEST_CODE == requestCode && RESULT_OK == resultCode) { //encode the image Uri uri = data.getData(); try { //get the image path String[] projection = {MediaStore.Images.Media.DATA}; CursorLoader cursorLoader = new CursorLoader(this,uri,projection,null,null,null); Cursor cursor = cursorLoader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); Log.d(TAG,"real path: "+path); encode(path); } catch (Exception ex) { Log.e(TAG, "failed." + ex.getMessage()); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
2.将图片转换成bitmap并编码
private void encode(String path) { //decode to bitmap Bitmap bitmap = BitmapFactory.decodeFile(path); Log.d(TAG, "bitmap width: " + bitmap.getWidth() + " height: " + bitmap.getHeight()); //convert to byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] bytes = baos.toByteArray(); //base64 encode byte[] encode = Base64.encode(bytes,Base64.DEFAULT); String encodeString = new String(encode); mTvShow.setText(encodeString);}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
3.将大串还原成图片
public void onDecodeClicked(View view) { byte[] decode = Base64.decode(mTvShow.getText().toString(),Base64.DEFAULT); Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length); //save to image on sdcard saveBitmap(bitmap); } private void saveBitmap(Bitmap bitmap) { try { String path = Environment.getExternalStorageDirectory().getPath() +"/decodeImage.jpg"; Log.d("linc","path is "+path); OutputStream stream = new FileOutputStream(path); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream); stream.close(); Log.e("linc","jpg okay!"); } catch (IOException e) { e.printStackTrace(); Log.e("linc","failed: "+e.getMessage()); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
需要注意的是,一张图片的编码速度会很慢,如果图片很大就更慢了。毕竟手机的处理能力有限。不过decode的速度确实相当的快,超出你的想象。好了,就是这样简单,今天就到这里了,晚安!
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow