Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将相机拍摄的图像以实际大小发送到服务器?_Java_Android_Android Camera_Android Camera Intent - Fatal编程技术网

Java 如何将相机拍摄的图像以实际大小发送到服务器?

Java 如何将相机拍摄的图像以实际大小发送到服务器?,java,android,android-camera,android-camera-intent,Java,Android,Android Camera,Android Camera Intent,它以低质量压缩,但我想发送高质量的图像 bitmap = BitmapFactory.decodeFile(str); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 40, stream); byte[] byte_v = stream.toByteArray();

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

         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();

    }
用100代替40

 bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
如果您想将实际拍摄的图像传送到服务器,您需要创建 形象

试试这个代码

Delcare这个变量

private String actualPictureImagePath = "";
然后在按钮上调用此方法,单击
cameraIntent()

然后在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)摄像头传输

还有一个小细节:将压缩格式从
PNG
更改为
JPEG
它将提高处理速度,如果不进行压缩,它无法生成正确的base64字符串,并且如果我压缩它,会得到低质量的图像,如果我压缩图像格式,请提供低质量的sir,myBitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);只需使用quality to 100它实际上不会压缩那么多图像,请检查myBitmap是否具有高质量。我为您更新了我的答案,请检查备用部分No sir compress方法的质量非常低,这没有帮助
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);