zoukankan      html  css  js  c++  java
  • 图片转成base64编码

    IOS:

    // UIImage的图片转成Base64编码字符串:
    
    UIImage *originImage = [UIImage imageNamed:@"originImage.png"];
    NSData *data = UIImageJPEGRepresentation(originImage, 1.0f);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    
    // Base64编码字符串转UIImage的图片:
    
    NSData *decodedImageData = [[NSData alloc] 
    initWithBase64EncodedString:encodedImageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
    UIImage *decodedImage = [UIImage imageWithData:decodedImageData];

    android:

    //将图片转换成base64编码
    
        private void encode(String path) {
                        //decode to bitmap
                        Bitmap bitmap = BitmapFactory.decodeFile(path);
                        Log.d(TAG, "bitmap  " + 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);
        }
    //将base64还原成图片
    
        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());
            }
        }
  • 相关阅读:
    材料用词积累
    SqlServer 数据库/数据表 拆分(分布式)【转】
    SqlServer 数据库读写分离【转】
    (整理)在REHL6.5上部署ASP.NET MVC
    (整理)MySQL_REHL6.5 安装MySQL5.5
    (转)查看SQLServer最耗资源时间的SQL语句
    (转)SQLServer查询数据库各种历史记录
    (转)SqlServer2008 数据库同步:发布、订阅
    (整理)SQL Server 2008 CDC 功能使用
    (整理)EF分页的实现
  • 原文地址:https://www.cnblogs.com/xyptechnology/p/7872087.html
Copyright © 2011-2022 走看看