Android:自定义照片尺寸

时间:2012-10-29 06:06:07

标签: android android-camera image-resizing android-photos

我有一个应用程序,其中移动捕获照片然后上传到服务器。我想首先检查照片的大小,然后根据实际照片尺寸将照片尺寸缩小到某个特定尺寸。实际上在服务器上它不允许具有大于某个特定大小(例如200kb)的照片。

或者我可以限制相机仅以特定尺寸拍摄照片,即使有人更改了相机的设置。

这里有很多问题要求检索照片的宽度和高度。但我想以字节自定义大小。

要打开相机我使用cameraIntent

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

3 个答案:

答案 0 :(得分:2)

这是唯一可能的解决方案AFAIK,以减小尺寸..

public void onActivityResult(int requestcode, int resultcode, Intent data) {
        if (resultcode == RESULT_OK) {
            if (requestcode == SELECT_PICTURE) {
                Uri uri = data.getData();
                String imagePath = getPath(uri);
                int SCALE = 2;
                try{

                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = SCALE;
                Bitmap bitmap= BitmapFactory.decodeFile(imagePath, o2);
                OutputStream os = new FileOutputStream(imagePath);
                bitmap.compress(Bitmap.CompressFormat.PNG, 85, os);
                os.flush();
                os.close();
                File file = new File(imagePath);
                Log.i("Bitmap", "Height Width "+bitmap.getHeight()+" "+bitmap.getWidth());
                Log.i("File","Size in bytes "+file.length());
                while(file.length()>100*1024)
                {
                    SCALE +=2;
                    o2.inSampleSize = SCALE;
                    bitmap= BitmapFactory.decodeFile(imagePath, o2);
                    os = new FileOutputStream(imagePath);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 85, os);
                    os.flush();
                    os.close();
                    file = new File(imagePath);
                    Log.i("Bitmap", "Height Width "+bitmap.getHeight()+" "+bitmap.getWidth());
                    Log.i("File","Size in bytes "+file.length());
                }
                bitmap = BitmapFactory.decodeFile(imagePath, o2);

                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                txtimagepath.setText(imagePath);

            }
        }
    }



protected String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int columnIndex = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(columnIndex);

    }

答案 1 :(得分:1)

您无法动态指定生成的文件的大小并进行缩放。您必须重新调整高度和宽度,这反过来会减小文件大小。

可能是一种反复出现的方法,最终可以将文件大小保持在您的限制之内,这将有所帮助。我想不出任何其他解决方案。至少,Android SDK不提供这样的API。

如果你想保留宽度/高度,那么你可以尝试压缩技术,这会导致质量损失,最终导致文件大小。但同样,没有直接的API来实现这一目标。

答案 2 :(得分:0)

Bitmap bt=Bitmap.createScaledBitmap(YourOrignalBitmap,320, 300, false);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bt.compress(Bitmap.CompressFormat.PNG,100, bos);

在这里你可以修复尺寸并保持图像质量..

相关问题