安卓:自定义照片尺寸 [英] Android: Customize photo size

查看:218
本文介绍了安卓:自定义照片尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中移动采集照片,然后上传到服务器一个应用程序。我想先检查照片的大小,然后减少照片大小取决于实际的照片尺寸某些特定的大小。其实在服务器上它不能让照片具有比一些特定的尺寸更大(比如200 KB)。

或者我可以限制摄像机捕捉到的照片只在特定的大小甚至有人改变摄像头的设置。

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

要打开相机,我的使用ca​​meraIntent

 意图cameraIntent =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
startActivityForResult(cameraIntent,CAMERA_REQUEST);


解决方案

这是唯一可能的解决方案据我所知,以减少大小。

 公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == RESULT_OK){
            如果(要求code == SELECT_PICTURE){
                URI URI = data.getData();
                串的ImagePath =的getPath(URI);
                int标= 2;
                尝试{                BitmapFactory.Options O2 =新BitmapFactory.Options();
                o2.inSampleSize = SCALE;
                位图位图= BitmapFactory.de codeFILE(的ImagePath,O2);
                OutputStream的OS =新的FileOutputStream(的ImagePath);
                bitmap.com preSS(Bitmap.Com pressFormat.PNG,85,OS);
                os.flush();
                os.close();
                档案文件=新的文件(的ImagePath);
                Log.i(位图,高度宽度+ bitmap.getHeight()++ bitmap.getWidth());
                Log.i(文件,大小以字节为单位+ file.length());
                而(file.length()→100 * 1024)
                {
                    SCALE + = 2;
                    o2.inSampleSize = SCALE;
                    位= BitmapFactory.de codeFILE(的ImagePath,O2);
                    OS =新的FileOutputStream(的ImagePath);
                    bitmap.com preSS(Bitmap.Com pressFormat.PNG,85,OS);
                    os.flush();
                    os.close();
                    文件=新的文件(的ImagePath);
                    Log.i(位图,高度宽度+ bitmap.getHeight()++ bitmap.getWidth());
                    Log.i(文件,大小以字节为单位+ file.length());
                }
                位= BitmapFactory.de codeFILE(的ImagePath,O2);                }
                赶上(例外五){
                    e.printStackTrace();
                }
                txtimagepath.setText(的ImagePath);            }
        }
    }保护字符串的getPath(URI URI){
        的String [] =投影{MediaStore.Images.Media.DATA};
        光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
        INT参数:columnIndex =光标
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        返回cursor.getString(参数:columnIndex);    }

I have one application in which mobile capture photo then it upload to the server. I want to check the size of photo first then reduce photo size to some particular size depending on actual photo size. Actually on server it not allow photo which have greater than some particular size (say 200 kb).

Or can I restrict the camera to capture photo only in particular size even some one change the setting of camera.

lot of question ask here which retrieve width and height of the photo. But I want to customize size in byte.

To open the camera I an using cameraIntent

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

解决方案

This is the only possible solution AFAIK, to reduce the size..

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

    }

这篇关于安卓:自定义照片尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆