2012-09-05 75 views
0

我讀了很多使用com.android.camera.action.CROP的例子,但他們都說要從Galery或相機裁剪圖像..任何人都可以告訴我如何使用com.android.camera.CROP裁剪位圖? 我曾嘗試過很多方法,但仍然失敗.. 我曾嘗試將位圖保存到文件,並從該文件創建一個uri變量,並使用uri變量作爲com.android.camera.action.CROP中的數據。 。但它還是失敗...... TT如何使用com.android.camera.action.CROP來裁剪位圖

這是我的代碼

public class CobaSaveImageActivity extends Activity { 
public ImageView tampilan; 
public static Bitmap bmp; 
public Uri mImageCaptureUri; 
int i = 1; 
File f; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    tampilan = (ImageView)findViewById(R.id.imageView1); 
    //bmp = BitmapFactory.decodeFile("/mnt/sdcard/bluetooth/enigma.bmp"); 
    bmp = BitmapFactory.decodeFile("/mnt/sdcard/enigma.jpg"); 
    tampilan.setImageBitmap(bmp); 
} 
public void save (View v){ 
    f = new File(Environment.getExternalStorageDirectory()+"/image/save"+i+".jpg"); 
    if (f.exists()) fileCheker(f); 
    try { 
     FileOutputStream fos = new FileOutputStream(f); 
     BufferedOutputStream bit = new BufferedOutputStream(fos);   
     bmp.compress(Bitmap.CompressFormat.JPEG, 50, bit); 
     bit.flush(); 
     bit.close(); 
     //bmp.recycle(); 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse 
       ("file://" + Environment.getExternalStorageDirectory()))); 
     Toast.makeText(this, "save complete to "+f.toString(), Toast.LENGTH_LONG).show(); 
     mImageCaptureUri = Uri.fromFile(f); 
     doCrop(); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public void fileCheker(File in){ 
    i++; 
    f = new File(Environment.getExternalStorageDirectory()+"/image/save"+i+".jpg"); 
    if (f.exists()) fileCheker(f); 
} 

public static Bitmap grayscale (Bitmap bmp){ 
    int height, width; 
    int pixel, A, R, G, B; 
    width = bmp.getWidth(); 
    height = bmp.getHeight(); 

    Bitmap bmpGray = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 

    for (int i =0;i<width;++i){ 
     for(int j=0;j<height;++j){ 
       pixel = bmp.getPixel(i,j); 
       A = Color.alpha(pixel); 
       R = Color.red(pixel); 
       G = Color.green(pixel); 
       B = Color.blue(pixel); 
       R = G = B = (int)((R+G+B)/3); 
       bmpGray.setPixel(i, j, Color.argb(A, R, G, B)); 
     } 
    } 
    return bmpGray; 
} 

public void gray(View v){ 
    new backtask().execute(); 
    //bmp = grayscale(bmp); tampilan.setImageBitmap(bmp); 
    // 
} 


public class backtask extends AsyncTask<Void, Void, Void>{ 
    //Bitmap temp; 
    ProgressDialog prog; 
    @Override 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     prog = ProgressDialog.show(CobaSaveImageActivity.this, "", "Progress...",true); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     bmp = grayscale(bmp); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result){ 
     super.onPostExecute(result);  
     prog.dismiss(); 
     tampilan.setImageBitmap(bmp); 
    } 
} 

private void doCrop() { 
    Intent intent = new Intent("com.android.camera.action.CROP"); 
    intent.setType("image/*"); 

    intent.setData(mImageCaptureUri); 
    //intent.putExtra("crop", true); 
    intent.putExtra("outputX", 200); 
    intent.putExtra("outputY", 200); 
    intent.putExtra("aspectX", 1); 
    intent.putExtra("aspectY", 1); 
    intent.putExtra("scale", true); 
    intent.putExtra("return-data", true); 

    startActivityForResult(intent, 1); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    if (resultCode != RESULT_OK) return; 

    switch (requestCode){ 
    case 1 : 
     Bundle extras = data.getExtras(); 
     if (extras != null){ 
      Bitmap crop = extras.getParcelable("data"); 
      tampilan.setImageBitmap(crop); 
     } 
    break; 
    } 
} 

}

回答

2

該意圖是不公開的Android API的一部分,並不能保證任何設備製造商來實現。在Android 1.x和早期的2.x設備上發現這種情況很常見,但之後就已經崩潰了。

您最好使用像Bitmap.createBitmap()Bitmap.createScaledBitmap()這樣的方法來創建原始圖像的調整大小或裁剪後的版本。

+0

對,這從未公開過。如果你真的想在你的應用中使用它,你最好的選擇就是複製應用中的代碼並根據需要進行修改。只有幾個類,它們可以稍作修改使用。 –

+0

我在哪裏可以得到代碼?代碼是否打開? –