上传前压缩相机图像

Dav*_*vid 26 compression upload camera android image

我正在使用此代码(来自www.internetria.com)拍摄照片并上传到服务器:

的onCreate:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri output = Uri.fromFile(new File(foto));
intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
startActivityForResult(intent, TAKE_PICTURE);
Run Code Online (Sandbox Code Playgroud)

onActivityResult:

ImageView iv = (ImageView) findViewById(R.id.imageView1);
        iv.setImageBitmap(BitmapFactory.decodeFile(foto));

        File file = new File(foto);
        if (file.exists()) {
            UploaderFoto nuevaTarea = new UploaderFoto();
            nuevaTarea.execute(foto);
        }
        else
            Toast.makeText(getApplicationContext(), "No se ha realizado la foto", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)

UploaderFoto:

ProgressDialog pDialog;
String miFoto = "";

@Override
protected Void doInBackground(String... params) {
    miFoto = params[0];
    try { 
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost httppost = new HttpPost("http://servidor.com/up.php");
        File file = new File(miFoto);
        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody foto = new FileBody(file, "image/jpeg");
        mpEntity.addPart("fotoUp", foto);
        httppost.setEntity(mpEntity);
        httpclient.execute(httppost);
        httpclient.getConnectionManager().shutdown();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

我想压缩图像,因为尺寸太大.

我不知道如何添加bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);到我的应用程序

Mat*_*sly 36

看看这里:ByteArrayOutputStream到FileBody

这些方面的东西应该有效:

更换

File file = new File(miFoto);
ContentBody foto = new FileBody(file, "image/jpeg");
Run Code Online (Sandbox Code Playgroud)

Bitmap bmp = BitmapFactory.decodeFile(miFoto)
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 70, bos);
InputStream in = new ByteArrayInputStream(bos.toByteArray());
ContentBody foto = new InputStreamBody(in, "image/jpeg", "filename");
Run Code Online (Sandbox Code Playgroud)

如果文件大小仍然是一个问题,您可能需要缩放图片以及压缩它.

  • ContentBody foto = new ByteArrayBody(bos.toByteArray(),filename); (6认同)

AZ_*_*AZ_ 5

将图像转换为Google WebP格式将节省大量字节,请参阅以下两篇文章,也可以在服务器端将webP转换为JPG / PNG / GIF。

Google WebP API的Java包装器

如何检出Google WebP库并在Android中将其用作本机库

首先,您需要从位图获取像素。

Bitmap bitmap = BitmapFactory.decodeFile(filePath);

int bytes = bitmap.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bitmap.copyPixelsToBuffer(buffer);
byte[] pixels = buffer.array();
Run Code Online (Sandbox Code Playgroud)

然后,您可以获得WebP字节数组。

int stride = bytes / height;
int quality = 100;
byte[] encoded = libwebp.WebPEncodeRGBA(pixels, width, height, stride, quality);
Run Code Online (Sandbox Code Playgroud)

Test.png(大小:106KB) Test.png(大小:106KB) Test.webp(大小:48KB) Test.webp(大小:48KB)