2013-05-21 50 views
0

在我的應用程序中,我需要從圖庫/相機拍攝圖像,裁剪這些圖像,然後將裁剪後的圖像保存在其他地方。下面的代碼可以完成大部分工作,但無法裁剪圖像以符合我的喜好。使用下面的代碼,我可以使用4個座標,圖像中間座標的頂部,底部,左側和右側裁剪圖像;但我需要使用8個座標進行裁剪。 This image顯示我的意思。如何在android中自定義圖像裁剪

public class MainActivity extends Activity { 
    private static final int PICK_FROM_CAMERA = 1; 
    private static final int PICK_FROM_GALLERY = 2; 
    private static final int PRESS_OK = 3; 
    ImageView imgview; 
    String m_path; 
    Bitmap m_thePic; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    imgview = (ImageView) findViewById(R.id.imageView1); 
    Button buttonCamera = (Button) findViewById(R.id.btn_take_camera); 
    Button buttonGallery = (Button) findViewById(R.id.btn_select_gallery); 
    Button buttonOk = (Button) findViewById(R.id.btn_ok); 

    File folder = new File(Environment.getExternalStorageDirectory().toString() + "/Images/"); 
    folder.mkdirs(); 
    buttonCamera.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     // call android default camera 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, 
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 
     // ******** code for crop image 
     intent.putExtra("crop", "true"); 
     intent.putExtra("aspectX", 0); 
     intent.putExtra("aspectY", 0); 
     intent.putExtra("outputX", 100); 
     intent.putExtra("outputY", 150); 

     try { 
      intent.putExtra("return-data", true); 
      startActivityForResult(intent, PICK_FROM_CAMERA); 
     } catch (ActivityNotFoundException e) { 
      // Do nothing for now 
     } 
     } 
    }); 
    buttonGallery.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Intent intent = new Intent(); 
     // call android default gallery 
     intent.setType("image/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     // ******** code for crop image 
     intent.putExtra("crop", "true"); 
     intent.putExtra("aspectX", 0); 
     intent.putExtra("aspectY", 0); 
     intent.putExtra("outputX", 200); 
     intent.putExtra("outputY", 150); 

     try { 
      intent.putExtra("return-data", true); 
      startActivityForResult(Intent.createChooser(intent, "Complete action using"), 1); 
     } catch (ActivityNotFoundException e) { 
      // Do nothing for now 
     } 
     } 
    }); 
    buttonOk.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     String m_path = Environment.getExternalStorageDirectory().toString(); 
     File m_imgDirectory = new File(m_path + "/Images/"); 
     File m_file = new File(m_path); 
     String m_fileid = "nm_tech" + System.currentTimeMillis() + ""; 
     m_file = new File(m_path, "/Images/" + m_fileid + ".jpg"); 
     Uri outputFileUri = Uri.fromFile(m_file); 

     Intent intent = new Intent(MainActivity.this, 
            ImageGalleryDemoActivity.class); 
     intent.putExtra("image", m_fileid); 
     startActivity(intent); 
     // startActivityForResult(intent,PRESS_OK); 
     // call android default camera 
     // Toast.makeText(getApplicationContext(), ,1234).show(); 
     } 
    }); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Bundle extras = data.getExtras(); 
    Bitmap m_thePic = extras.getParcelable("data"); 
    String m_path = Environment.getExternalStorageDirectory().toString(); 
    File m_imgDirectory = new File(m_path + "/Images/"); 
    if (!m_imgDirectory.exists()) { 
     m_imgDirectory.mkdir(); 
    } 
    OutputStream m_fOut = null; 
    File m_file = new File(m_path); 
    m_file.delete(); 
    String m_fileid = "nm_tech" + System.currentTimeMillis() + ""; 
    m_file = new File(m_path, "/Images/" + m_fileid + ".jpg"); 
    try { 
     if (!m_file.exists()) { 
     m_file.createNewFile(); 
     } 
     m_fOut = new FileOutputStream(m_file); 
     Bitmap m_bitmap = m_thePic.copy(Bitmap.Config.ARGB_8888, true); 
     m_bitmap.compress(Bitmap.CompressFormat.PNG, 100, m_fOut); 
     m_fOut.flush(); 
     m_fOut.close(); 
     MediaStore.Images.Media.insertImage(getContentResolver(), 
         m_file.getAbsolutePath(), 
              m_file.getName(), 
              m_file.getName()); 
    } catch (Exception p_e) { 
    } 

    if (requestCode == PICK_FROM_CAMERA) { 
     if (extras != null) { 
     // Bitmap photo = extras.getParcelable("data"); 
     imgview.setImageBitmap(m_thePic); 
     } 
    } 

    if (requestCode == PICK_FROM_GALLERY) { 
     // Bundle extras2 = data.getExtras(); 
     if (extras != null) { 
     imgview.setImageBitmap(m_thePic); 
     } 
    } 

    if (requestCode == PRESS_OK) { 
     Bundle extras11 = data.getExtras(); 
     Bitmap bmp = (Bitmap) extras.get("data"); 

     /* 
     * Bitmap photo = extras.getParcelable("data"); 
     * imgview.setImageBitmap(photo); Intent n=new 
     * Intent(getApplicationContext(),ImageGalleryDemoActivity.class); 
     * n.putExtra("data",photo); startActivity(n); 
     */ 
    } 
    } 
} 
+0

圖片在保存時必須是矩形,所以你想成爲空白區域?透明,黑色?你可以通過你的觀點,找到保存圖像所需的最小Rect,然後在你的新圖像上繪製你的圖像部分,然後使用路徑或任何你填寫的東西,像它刪除你的側面的空矩形。一點點的數學,但... –

+0

thnkyou,空白區域不是我prblm ...任何我想填補透明noprblm ...正常使用我的代碼1可以使用4點左,上,右,底部,看到我的圖像一次在這裏8點是threre使用這些字體我必須裁剪我的圖像 – user2401554

回答

0

這裏有一個簡單的方法供您座標的陣列創建最低需要RECT

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.activity_main); 
    Rect r = getRect(new int[]{10, 10, 20, 20, 30, 30}, new int[]{20, 100, 10, 110, 20, 100}); 
    System.out.println(r.left + " " + r.top + " " + r.bottom + " " + r.right); 
} 

public Rect getRect(int[] x, int y[]){ 
    Rect r = new Rect(); 
     // Set the first coordinate, in order not to include 0, 0 
    r.set(x[0], y[0], x[0], y[0]); 
    for(int i = 1; i < x.length; i++){ 
     r.union(x[i], y[i]); 
    } 
    return r; 
} 

編輯:看看你有8分,只需撥打這個getRect這樣

Rect rectToDraw = getRect(new int{yourx1, yourx2, yourx3, yourx4, yourx5, yourx6, yourx7, yourx8,}, new int{youry1, youry2, youry3, youry4, youry5, youry6, youry7, youry8}); 

你可以通過這個功能屁股你想要的很多點,而不僅僅是8

希望這有助於nd享受你的工作

+0

謝謝你,我沒有得到你在哪裏你給圖像路徑 – user2401554

+0

這只是爲所有的座標定義一個普通的矩形,這裏是我粘貼示例數組''Rect r = getRect(new int [] {10,10,20,20,30,30},new int [] {20,100,10,110,20,100});'' 。然後從rect得到底部,左邊,頂部和右邊。正如我所見,你可以自己畫畫。 –

+0

我必須裁剪圖像基於8點,就像那個樣本圖像,我不能讓你可以建議我如何解決我的prblm – user2401554