如何在android中裁剪图像

时间:2013-11-02 16:29:17

标签: android image opencv crop

嗨,大家好我正在开发一个Android应用程序(浊度计应用程序),它可以捕获穿过水样的光线并测量从水样中出来的光量,但我发现我必须能够裁剪它捕获的图像以获得正确的读数可以有人帮助PLIZ。这是我的代码行。因此,当我测量从清水中出来的光量时,我得到的值很高,即46(NTU),而对于清洁水,值必须低于5(NTU)是SI单位。所以我被告知算法是在计算整个图像,而不是只计算感兴趣的区域,这是图像的亮区。

PictureCallback callback = new PictureCallback() {
       @Override
       public void onPictureTaken(byte[] data, Camera camera) {
           Log.i(TAG, "Saving a bitmap to file");


               if (OpenCVLoader.initDebug()) { 

               Log.d("work", "work");

                  Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
               Log.i("camera open", "n");

               imgToProcess=new Mat();


                  Utils.bitmapToMat(picture, imgToProcess);

                    Log.d("work", "work");

               Imgproc.cvtColor(imgToProcess, imgToProcess, Imgproc.COLOR_RGB2GRAY);

                t = Core.mean(imgToProcess).toString();

1 个答案:

答案 0 :(得分:0)

以下是在我的应用中完成工作的代码的一部分:

private void doCrop() {

        final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

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

        List<ResolveInfo> list = getPackageManager().queryIntentActivities(
                intent, 0);

        int size = list.size();

        if (size == 0) {
            Toast.makeText(this, res.getString(R.string.no_crop_app),
                    Toast.LENGTH_SHORT).show();

            return;
        } else {
            intent.setData(mImageCaptureUri);

            intent.putExtra("outputX", 256);
            intent.putExtra("outputY", 256);
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("scale", true);
            intent.putExtra("return-data", true);

            if (size == 1) {
                Intent i = new Intent(intent);
                ResolveInfo res = list.get(0);

                i.setComponent(new ComponentName(res.activityInfo.packageName,
                        res.activityInfo.name));

                startActivityForResult(i, ACTIVITY_CROP_FROM_CAMERA);
            } else {
                for (ResolveInfo res : list) {
                    final CropOption co = new CropOption();

                    co.title = getPackageManager().getApplicationLabel(
                            res.activityInfo.applicationInfo);
                    co.icon = getPackageManager().getApplicationIcon(
                            res.activityInfo.applicationInfo);
                    co.appIntent = new Intent(intent);

                    co.appIntent
                    .setComponent(new ComponentName(
                            res.activityInfo.packageName,
                                res.activityInfo.name));

                    cropOptions.add(co);
                }

                CropOptionAdapter adapter = new CropOptionAdapter(
                        getApplicationContext(), cropOptions);

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(res.getString(R.string.get_crop_app));
                builder.setAdapter(adapter,
                        new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            startActivityForResult(
                                    cropOptions.get(item).appIntent,
                                    ACTIVITY_CROP_FROM_CAMERA);
                        }
                });

                builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        if (mImageCaptureUri != null) {
                            getContentResolver().delete(mImageCaptureUri, null,
                                    null);
                            mImageCaptureUri = null;
                        }
                    }
                });

                AlertDialog alert = builder.create();

                alert.show();
            }
        }

}

CropOption类:

public class CropOption {
            public CharSequence title;
            public Drawable icon;
            public Intent appIntent;

}

和相应的适配器:

 class CropOptionAdapter extends ArrayAdapter<CropOption> {
    private ArrayList<CropOption> mOptions;
    private LayoutInflater mInflater;

    public CropOptionAdapter(Context context, ArrayList<CropOption> options) {
            super(context, R.layout.crop_selector, options);
            mOptions        = options;
            mInflater       = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup group) {
            if (convertView == null)
                    convertView = mInflater.inflate(R.layout.crop_selector, null);

            CropOption item = mOptions.get(position);

            if (item != null) {
                    ((ImageView) convertView.findViewById(R.id.iv_crop_icon)).setImageDrawable(item.icon);
                    ((TextView) convertView.findViewById(R.id.tv_crop_name)).setText(item.title);

                    return convertView;
            }

            return null;
    }

}