2

我正在尝试将图像上传到服务器而不进行压缩,以保持完整的质量。

我找到了以下方法:

compress(Bitmap.CompressFormat.JPEG, 100, bao);

但在我的情况下它不起作用。原始图像是 2 MB,但在服务器上它只有 60 KB。

图像捕获使用以下代码

Intent i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,camdata);


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK) {
        Bundle bn=data.getExtras();
        bm=(Bitmap)bn.get("data");
        imageview.setImageBitmap(bm);
        imageview.setClickable(false);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize=6;
        final float scale=getResources().getDisplayMetrics().density;
        Display dis=getWindowManager().getDefaultDisplay();
        int width=dis.getWidth();
        scalebmp=Bitmap.createScaledBitmap(bm,400,300, true);
        scalebmp.compress(Bitmap.CompressFormat.JPEG, 100, bao);

        byte [] ba = bao.toByteArray();
        String imgstr=Base64.encodeToString(ba,Base64.DEFAULT);
    }
}
4

3 回答 3

3
scalebmp.compress(Bitmap.CompressFormat.PNG, 100, bao);

使用上线而不是下线

scalebmp.compress(Bitmap.CompressFormat.JPEG, 100, bao);

看到 JPEG/JPG 是无损图像,PNG 永远不会失去压缩质量,希望它对你有用

于 2014-05-26T11:13:42.277 回答
2

将 options.inSampleSize 的值从 6 更改为 1

尝试这个

options.inSampleSize = 1;

代替

 options.inSampleSize=6;

快乐的编码!

于 2014-08-28T12:23:49.207 回答
0

初始化 Uri

Uri selectedImage;
private final int PICK_IMAGE_CAMERA = 1

在 Oncreate 中编写以下代码以忽略异常

StrictMode.VmPolicy.Builder builder1 = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder1.build());

将以下代码放入您的相机侦听器中

final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Your Folder Name/";
                            File newdir = new File(dir);
                            newdir.mkdirs();

                            String file = dir + DateFormat.format("yyyy-MM-dd_hhmmss", new Date()).toString() + ".jpg";

                            File newfile = new File(file);
                            try {
                                newfile.createNewFile();
                            } catch (IOException ignored) {

                            }

                            selectedImage = Uri.fromFile(newfile);

                            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, selectedImage);
                            startActivityForResult(cameraIntent, PICK_IMAGE_CAMERA);

将以下代码放入 onActivityResult

try {
                        if (selectedImage != null) {
                            if (selectedImage.toString().startsWith("file:")) {

                                Your File Name = new File(selectedImage.getPath());

                                InputStream in = null;
                                try {
                                    final int IMAGE_MAX_SIZE = 1200000;
                                    in = getContentResolver().openInputStream(selectedImage);
                                    // Decode image size
                                    BitmapFactory.Options o = new BitmapFactory.Options();
                                    o.inJustDecodeBounds = true;
                                    BitmapFactory.decodeStream(in, null, o);
                                    try {
                                        in.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    int scale = 1;
                                    while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
                                            IMAGE_MAX_SIZE) {
                                        scale++;
                                    }
                                    Bitmap b = null;
                                    in = getContentResolver().openInputStream(selectedImage);
                                    if (scale > 1) {
                                        scale--;
                                        // scale to max possible inSampleSize that still yields an image
                                        // larger than target
                                        o = new BitmapFactory.Options();
                                        o.inSampleSize = scale;
                                        b = BitmapFactory.decodeStream(in, null, o);

                                        // resize to desired dimensions
                                        int height = b.getHeight();
                                        int width = b.getWidth();

                                        double y = Math.sqrt(IMAGE_MAX_SIZE
                                                / (((double) width) / height));
                                        double x = (y / height) * width;

                                        Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
                                                (int) y, true);
                                        b.recycle();
                                        b = scaledBitmap;

                                        System.gc();
                                    } else {
                                        b = BitmapFactory.decodeStream(in);
                                    }
                                    in.close();
                                    Your Image View.setImageBitmap(b);
                                } catch (Exception ignored) {
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
于 2018-05-03T07:25:11.880 回答