【问题标题】:how to crop bitmap using com.android.camera.action.CROP如何使用 com.android.camera.action.CROP 裁剪位图
【发布时间】:2012-08-30 11:41:44
【问题描述】:

我阅读了许多使用 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;
    }
}

}

【问题讨论】:

    标签: android image bitmap crop android-image


    【解决方案1】:

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

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

    【讨论】:

    • 对,这从未公开过。如果你真的想在你的应用程序中使用它,最好的办法是复制应用程序中的代码并根据需要进行修改。只有几个类,只需稍作修改即可使用。
    猜你喜欢
    • 2012-08-29
    • 1970-01-01
    • 2015-05-05
    • 2019-05-10
    • 2013-08-16
    • 1970-01-01
    • 2015-12-11
    • 2013-03-25
    • 2017-10-19
    相关资源
    最近更新 更多