【问题标题】:Android: Customize photo sizeAndroid:自定义照片大小
【发布时间】:2012-10-18 11:20:41
【问题描述】:

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

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

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

使用 cameraIntent 打开相机

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

【问题讨论】:

  • 当你改变照片的宽度和高度时,显然字节大小也会改变。你试过了吗??
  • 好的,我会试试这个,但我想保持照片的高度和宽度......
  • 我认为在高度和宽度不变的情况下改变尺寸是不可能的。有可能,但图像质量也会相应降低。

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


【解决方案1】:

这是 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);

    }

【讨论】:

  • 当然,它是一个字符串。我已经更新了答案,[仅供参考,因为您已经从相机/图库中获得了位图,我以为您知道......第二个但更重要的一个“?”会做..不需要4。]
  • 这个getPath(uri)方法是什么,这个方法的代码是什么??
  • thanx 4 以上代码,但存在一些问题。首先,不推荐使用 managedQuery 方法,并且由于这一行,它会抛出 NullPointerException
  • 它已被弃用,这就是它抛出 NPR 的原因?无论如何..工作正常刚刚再次测试。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:
    Bitmap bt=Bitmap.createScaledBitmap(YourOrignalBitmap,320, 300, false);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bt.compress(Bitmap.CompressFormat.PNG,100, bos);
    

    在这里您可以固定大小并保持图像质量..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多