2011-10-11 27 views
3

我研究了如何设置图片的大小,并且我偶然发现了CameraParameters和与之相关的setPictureSize方法。问题是,我无法弄清楚如何使用这些。我不知道需要导入什么,要创建什么对象,如何使用setPictureSize方法或甚至放置代码的位置。我有2块代码,我觉得它可能是一个使用setPictureSize的好地方。这些块是:使用setPictureSize和摄像机参数

takePicture = (Button) findViewById(R.id.takePicture); 

    takePicture.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if (name.getText().length() == 0 || experimentInput.getText().length() == 0) { 
       showDialog(DIALOG_REJECT); 
       return; 
      } 

      ContentValues values = new ContentValues(); 
      imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
      intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

      time = getTime() ; 

      startActivityForResult(intent, CAMERA_PIC_REQUESTED); 
     } 

    }); 

和:

public static File convertImageUriToFile (Uri imageUri, Activity activity) { 
    if (activity == null) Log.d("test", "NULL!"); 
    Cursor cursor = null; 
    try { 

     String [] proj={MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION}; 
     cursor = activity.managedQuery(imageUri, 
       proj, // Which columns to return 
       null,  // WHERE clause; which rows to return (all rows) 
       null,  // WHERE clause selection arguments (none) 
       null); // Order-by clause (ascending by name) 
     int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION); 
     if (cursor.moveToFirst()) { 
      @SuppressWarnings("unused") 
       String orientation = cursor.getString(orientation_ColumnIndex); 
      return new File(cursor.getString(file_ColumnIndex)); 
     } 
     return null; 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
} 

public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    if(cursor!=null) 
    { 
     //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL 
     //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
    else return null; 
} 

所以,我的问题是,我怎么使用setPictureSize和我应该在哪里放呢?没有网站,也没有Android开发指南,也没有任何其他相关的StackOverflow问题对我有帮助。

Editted代码:这是用来当你实际上是通过自定义View使用相机

public Bitmap getPath(Uri uri) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     if(cursor!=null) 
     { 
      //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL 
      //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 

      int desiredImageWidth = 100; // pixels 
      int desiredImageHeight = 100; // pixels 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inSampleSize = 2; // will cut the size of the image in half; OPTIONAL 
      Bitmap newImage = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(cursor.getString(column_index), o), 
                 desiredImageWidth, 
                 desiredImageHeight, 
                 false); 

      return newImage; //cursor.getString(column_index); 
     } 
     else return null; 
    } 

回答

2

。你在这里做的是向Android发送一个暗含的意图,以打开另一个使用相机拍摄照片的Activity

在这种情况下,您可以使用MediaStore.EXTRA_SIZE_LIMIT来限制图像的大小。如果要指定图像的尺寸,则必须从保存的URI中加载它,然后创建一个新的缩放图像,然后将其保存在旧图像上。

一旦你有图像的URI(这是从投影DATA属性),你可以调整图片的大小,像这样:

int desiredImageWidth = 100; // pixels 
int desiredImageHeight = 100; // pixels 
BitmapFactory.Options o = new BitmapFactory.Options(); 
o.inSampleSize = 2; // will cut the size of the image in half; OPTIONAL 
Bitmap newImage = Bitmap.createScaleBitmap(BitmapFactory.decodeFile(imagePath, o), 
              desiredImageWidth, 
              desiredImageHeight, 
              false); 

然后将其保存在旧的。

+0

这听起来很合理。我究竟在哪里把这个呢? 'convertImageUriToFile'方法或'getPath'方法中的某处? – Mxyk

+0

就个人而言,我不会把位图编码放在任何一种方法里面,因为每个服务器都有自己的目的。 'imagePath'必须是一个直接指向图像文件的String对象(不是android内容URI),并且它看起来像'getPath()'会为你准确检索。您还应该能够使用'getAbsolutePath()'或'getPath()'从'convertImageUriToFile()'方法返回的'File'对象获取这些信息。 – DeeV

+0

我有点理解你在说什么。要做你提到的,我必须使用一个String对象而不是android内容uri。我的问题是,作为新手的Android程序员,我需要做些什么才能做到这一点?我需要将String []投影放在某处/将它返回给某个东西吗? – Mxyk