如何使用com.android.camera.action.CROP裁剪位图

时间:2012-09-05 17:29:32

标签: android image bitmap crop android-image

我使用com.android.camera.action.CROP读了很多例子,但他们都说要从galery或相机裁剪图像..任何人都可以告诉我如何使用com.android.camera.CROP裁剪Bitmap? 我尝试了很多方法,但仍然失败了.. 我试图将位图保存到文件,并从该文件创建一个uri变量带,并使用uri变量作为com.android.camera.action.CROP的数据...但它仍然失败... T.T

这是我的代码

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

}

1 个答案:

答案 0 :(得分:3)

该Intent不是公共Android API的一部分,并不保证可由任何设备制造商实施。在Android 1.x和早期的2.x设备上常见,但此后一直在下降。

最好使用像Bitmap.createBitmap()Bitmap.createScaledBitmap()这样的方法来创建原始图像的调整大小或裁剪版本。