2011-10-11 29 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