0

我阅读了许多使用 com.android.camera.action.CROP 的示例,但他们都说要从画廊或相机中裁剪图像。任何人都可以告诉我如何使用 com.android.camera.CROP 裁剪位图?我尝试了很多方法,但仍然失败..我曾尝试将位图保存到文件,并从该文件创建一个 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;
    }
}

}

4

1 回答 1

3

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

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

于 2012-09-05T17:40:25.150 回答