将图像从摄像头意图中的+ 1mb调整为< = 150k Android

时间:2014-01-30 12:52:41

标签: android android-intent android-camera

我正在尝试减少从相机和图库中拍摄的照片尺寸(1 + mb到150k。两者)。

  1. 拍摄照片的代码:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
    //intent.putExtra("crop", "true");
    
    intent.putExtra("outputX", 50);
    
    intent.putExtra("outputY", 50);
    
    intent.putExtra("aspectX", 1);
    
    intent.putExtra("aspectY", 1);
    
    intent.putExtra("outputX", 128);
    
    intent.putExtra("outputY", 128);
    
    intent.putExtra("scaleUpIfNeeded", true);
    
    intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
    
    intent.putExtra("return-data", false);
    
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
    
    startActivityForResult(intent, PICTURE_RESULT);
    
  2. 意图从图库中选择

    Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    
    intent.setType("image/*");
    
    //intent.putExtra("crop", "true");
    
    intent.putExtra("outputX", 50);
    
    intent.putExtra("outputY", 50);
    
    intent.putExtra("aspectX", 1);
    
    intent.putExtra("aspectY", 1);
    
    intent.putExtra("scale", true); 
    
    intent.putExtra("scaleUpIfNeeded", true);
    
    intent.putExtra("return-data", true);
    
    intent.putExtra(MediaStore.EXTRA_OUTPUT,destination);
    intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
    
    startActivityForResult(Intent.createChooser(intent, "Choisir Photo"),SELECT_FILE);
    
  3. 我的活动结果如下

    @Override
    
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
            super.onActivityResult(requestCode, resultCode, data);
    
            if (resultCode == PICTURE_RESULT) {
    
                if (requestCode == REQUEST_CAMERA) {
    
                    Editer.PHOTO_FROM=11;
    
                    File f = new File(Environment.getExternalStorageDirectory()
                            .toString());
    
                    for (File temp : f.listFiles()) {
    
                        if (temp.getName().equals("temp.jpg")) {
    
                            f = temp;
    
                            break;
    
                        }
    
                    }
    
                    //Uri.fromFile(createFile());
    
                    Constant.filePath=f.getAbsolutePath();
    
                    String fname = "user_image_golf.jpg";
    
                    BitmapFactory.Options options = new BitmapFactory.Options();
    
                    options.inTempStorage = new byte[16 * 1024];
    
                    options.inSampleSize = 4;
    
                    options.outWidth = 100;
    
                    options.outHeight = 100;
    
                    String root = Environment.getExternalStorageDirectory().toString();
    
                    File myDir = new File(root + "user_image_golf.jpg");   // == /
                    myDir.mkdirs();
    
                    destination = new 
    File(Environment.getExternalStorageDirectory(),"user_image_golf.jpg");
    
    
                    Bitmap bitmap = BitmapFactory.decodeFile(destination.toString(), options);
    
                    Bitmap map = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
                    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    
                    map.compress(Bitmap.CompressFormat.PNG, 50, bao);
    
                    //byte[] ba = bao.toByteArray();
    
                    File file = new File (myDir, f.getAbsolutePath());
    
                    if (file.exists ()) file.delete ();
    
                    try {
    
                        FileOutputStream out = new FileOutputStream(file);
    
                        map.compress(Bitmap.CompressFormat.JPEG, 20, out);
    
                        out.flush();
    
                        out.close();
    
                    } catch (Exception e) {
    
                        e.printStackTrace();
                    }
    
                    System.out.println("//imagedata=Base64.encodeBytes(ba);");
    
                    ///imagedata=Base64.encodeBytes(ba);
    
                    //rotateImage(Uri.fromFile(destination).toString());
    
                }
    
    
                else if (requestCode == SELECT_FILE) {
    
                    Uri selectedImageUri = data.getData();
    
                    Constant.filePath =selectedImageUri.toString();
    
                    Editer.PHOTO_FROM=2;
    
                    System.out.println("getAbsolutePath() "+selectedImageUri);
    
                    System.out.println("selectedImageUri-getAbsolutePath()
     "+getPath(selectedImageUri,m));
    
    
                }
    
            }
            m.finish();
    
        }
    

    如果我从图库中选择照片或从相机拍摄照片,相机中的新照片或图库中最新选择的图像应覆盖保存在SD卡上的照片。

1 个答案:

答案 0 :(得分:1)

试试这个。

private void selectImage() {
    final CharSequence[] items = { "Take Photo", "Choose from Gallery" };

    AlertDialog.Builder builder = new AlertDialog.Builder(BVMobile.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                // folder stuff
                Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File imagesFolder = new File(Environment
                        .getExternalStorageDirectory(), "verificationapp");
                imagesFolder.mkdirs();

                Date alsoNow = Calendar.getInstance().getTime();
                tempfile = new SimpleDateFormat("yyyyMMddHHmmss")
                        .format(alsoNow);
                File image = new File(imagesFolder, tempfile + ".png");
                Uri uriSavedImage = Uri.fromFile(image);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        uriSavedImage);
                startActivityForResult(cameraIntent, REQUEST_CAMERA);

            } else if (items[item].equals("Choose from Gallery")) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(intent, SELECT_FILE);
            }
        }
    });
    builder.show();
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_FILE) {
            if (data != null) {

                Uri selectedImageUri = data.getData();

                selectedfile = getRealPathFromURI(selectedImageUri);

                Date alsoNow = Calendar.getInstance().getTime();
                String currentdate = new SimpleDateFormat("yyyyMMddHHmmss")
                        .format(alsoNow);

                imgname = currentdate + "_" + refid;

                    strPhoto1 = SERVER_FILE_PATH + imgname + ".png";




            }
        }  else if (requestCode == REQUEST_CAMERA) {

            File imagesFolder = new File(
                    Environment.getExternalStorageDirectory(),
                    "verificationapp");
            File image = new File(imagesFolder, tempfile + ".png");
            selectedfile = image.toString();

            imgname = tempfile + "_" + refid;

        }
    }
}

public String converttobase64(String picturePath) {
    String b64 = null;
    if (!picturePath.equals("")) {

        Bitmap bm = BitmapFactory.decodeFile(picturePath);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);

        byte[] b = baos.toByteArray();
        b64 = Base64.encodeToString(b, Base64.DEFAULT);

    }

    return b64;
}