上传前压缩相机图像

时间:2013-10-25 15:41:42

标签: android image upload camera compression

我正在使用此代码(来自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);

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

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;
}

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

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

4 个答案:

答案 0 :(得分:36)

请查看此处:ByteArrayOutputStream to a FileBody

这些方面应该有效:

替换

File file = new File(miFoto);
ContentBody foto = new FileBody(file, "image/jpeg");

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

如果文件大小仍然存在问题,您可能需要缩放图片以及压缩图片。

答案 1 :(得分:4)

将图像转换为Google WebP format它将节省大量字节,请参阅以下两篇文章,您也可以将webP转换为JPG / PNG / GIF,无论您想在服务器端如何。

Java Wrapper of Google WebP API

How to check out Google WebP library and use it in Android as native library

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

Bitmap bitmap = BitmapFactory.decodeFile(filePath);

int bytes = bitmap.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bitmap.copyPixelsToBuffer(buffer);
byte[] pixels = buffer.array();

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

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

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

答案 2 :(得分:1)

使用okhttp我像这样上传:

MediaType MEDIA_TYPE_PNG
                        = MediaType.parse("image/jpeg");

                //Compress Image
                Bitmap bmp = BitmapFactory.decodeFile(fileToUpload.getAbsolutePath());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.JPEG, 70, bos);

                RequestBody requestBody = new MultipartBuilder()
                        .type(MultipartBuilder.FORM)
                        .addFormDataPart("photo", fileToUpload.getName(), RequestBody.create(MEDIA_TYPE_PNG, bos.toByteArray()))
                        .build();

                request = new Request.Builder()
                        .url(urlToUploadTo)
                        .post(requestBody)
                        .build();

                try {
                    response = client.newCall(request).execute();
                    if (response != null) {
                        if (response.isSuccessful()) {
                            responseResult = response.body().string();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

答案 3 :(得分:-2)

查看$(document).ready(function(){ $( "#my_form" ).submit(function( e ) { //CHANGED using submit event. e.preventDefault(); var verb = $("#word1").val(); //CHANGED $.ajax({ url: "testrun.php", data: "verb="+verb, //CHANGED type: "POST", }); }); }); 方法:

compressImage()