【问题标题】:how to send image taking by camera to server in actual size?如何将相机拍摄的图像以实际尺寸发送到服务器?
【发布时间】:2018-06-19 11:43:42
【问题描述】:

它以低质量压缩,但我想发送高质量图像

         bitmap = BitmapFactory.decodeFile(str);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 40, stream);
        byte[] byte_v = stream.toByteArray();
        if(bitmap!=null)
        {
            bitmap.recycle();
        }

        encod = Base64.encodeToString(byte_v,Base64.DEFAULT);
        ImageMulti();

    }

【问题讨论】:

    标签: java android android-camera android-camera-intent


    【解决方案1】:

    设置 100 个,共 40 个

     bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    

    【讨论】:

    • 一个小细节:将 CompressFormat 从 PNG 更改为 JPEG 它将提高事务的速度
    【解决方案2】:

    如果您希望将相机拍摄的实际图像发送到服务器,您需要创建 图片

    试试这个代码

    删除这个变量

    private String actualPictureImagePath = "";
    

    然后在按钮点击时调用这个方法cameraIntent()

    private void cameraIntent() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = timeStamp + ".jpg";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        actualPictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
        File file = new File(pictureImagePath);
        Uri outputFileUri = Uri.fromFile(file);
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);               
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(cameraIntent, 1);
    }
    

    然后在onActivityResult() 处理这个

    @override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
        File imgFile = new  File(actualPictureImagePath);
            if(imgFile.exists()){        
           Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
           // Now use this bitmap to send to server 
           // Code to convert bitmap to Base64 
          ByteArrayOutputStream baos = new ByteArrayOutputStream();  
          myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm 
          is the bitmap object   
          byte[] byteArrayImage = baos.toByteArray(); 
          String encodedImage = Base64.encodeToString(byteArrayImage, 
          Base64.DEFAULT);
    
            }
        }
    
    }
    

    此问题的替代解决方案

    @override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == 1) {
            File imgFile = new  File(actualPictureImagePath);
                if(imgFile.exists()){        
              InputStream inputStream = null;//You can get an inputStream using any IO API
    inputStream = new FileInputStream(imgFile.getAbsolutePath());
    byte[] buffer = new byte[8192];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);
    try {
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            output64.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    output64.close();
    
    String base64String = output.toString();
    
                }
            }
    
        }
    

    这是用于 Base64 位图的代码

    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
              myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm 
              is the bitmap object   
              byte[] byteArrayImage = baos.toByteArray(); 
              String encodedImage = Base64.encodeToString(byteArrayImage, 
              Base64.DEFAULT);
    

    替代解决方案

    注意:-

    不要忘记在清单中添加运行时权限

    1)读写权限

    2)相机权限

    【讨论】:

    • 不压缩就不能生成正确的base64字符串,如果我压缩它给出低质量的图像,请帮忙
    • 如果我压缩图像格式它的质量很差先生,myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    • 只使用质量到 100 它实际上不会压缩图像那么多,请检查 myBitmap 是否具有高质量
    • 我已为您更新了答案,请查看备用部分
    • 没有先生压缩方法提供非常低的质量,它无济于事
    猜你喜欢
    • 2018-01-19
    • 2017-10-07
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    相关资源
    最近更新 更多