精华内容
下载资源
问答
  • 我们在平时使用图片的时候想要把多余的部分去掉,有时候甚至觉得如果图片换一种形状会更加好看的时候,我们应该...拉动图片上的方框对图片进行裁剪;还可以选择圆角颜色。完成后点击“确定”。 3、图片处理完成后,点

    我们在平时使用图片的时候想要把多余的部分去掉,有时候甚至觉得如果图片换一种形状会更加好看的时候,我们应该用什么样的方法呢?别急,今天我就教大家一款在线图片编辑软件https://www.yasuotu.com/editor)的使用方法,简单易操作,不需要下载。具体步骤如下:
    1、打开网站,点击选择图片。
    在这里插入图片描述

    2、在左侧的工具栏里裁剪比例、裁剪高度、裁剪宽度、圆角比例等输入参数;还可以通过拖动下方的圆球进行修改;拉动图片上的方框对图片进行裁剪;还可以选择圆角颜色。完成后点击“确定”。
    在这里插入图片描述

    3、图片处理完成后,点击“保存本地”即可。
    在这里插入图片描述

    根据以上的操作步骤,就可以自由裁剪图片形状,在线就可以操作;如果你还想对图片进一步进行修图还可以使用美化图片、色彩曲线、色彩调整、图片去底色等功能。想要了解更多详情请点击收藏。

    展开全文
  • 主要介绍了Android 以任意比例裁剪图片的相关资料,非货不错,具有参考借鉴价值,需要的朋友可以参考下
  • iOS 图片任意比例裁剪,Swift版。可以在图片选取后,调用裁剪管理类的裁剪方法,完成后,会回调裁剪后的图片
  • 裁剪任意大小图片

    2014-07-22 17:23:32
    图片裁剪功能: 主要功能拍照后裁剪图片和选择本地图片文件裁剪,工程会根据裁剪区等比例生成新图片(图片不会被压缩,不用调用系统裁剪功能哦)
  • C++怎么任意裁剪图片

    2017-08-14 17:43:53
    问一下各位大神们,怎么在VS2015中使用C++裁剪n张图片并拼合在一起?
  • 主要为大家详细介绍了php+js实现裁剪任意形状图片,类似css mask,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • MATLAB裁剪图片

    千次阅读 2019-03-06 19:45:09
    考试报名需要上传照片,但是大小比例有要求,高宽比要求为4:3,用MATLAB来进行裁剪。 1. 暴力法 原图为护照照片,比例为高1080,宽1626,需要将宽变为810,采用逐个值代替法。 注意需要将值变为uint8才可以保存成...

    考试报名需要上传照片,但是大小比例有要求,高宽比要求为4:3,用MATLAB来进行裁剪。

    1. 暴力法

    原图为护照照片,比例为高1080,宽1626,需要将宽变为810,采用逐个值代替法。

    注意需要将值变为uint8才可以保存成图片并正确显示,double不可。

    clear all;
    clc;
    a00 = imread('C:\Users\Administrator\Desktop\lk.jpg');
    b = zeros(1080,810,3);
    dimI = size(a00);
    rm = dimI(1);
    cm = dimI(2);
    km = dimI(3);
    
    for k = 1:km
        for i = 1:rm
            for j = 1:810
                b(i,j,k) = uint8(a00(i,j+403,k));
                %b(i,j,k) = a00(i,j,k);
            end
        end
    end
    b_u = uint8(b);
    imshow(b_u);
    imwrite(b_u,'C:\Users\Administrator\Desktop\lk3.jpg','jpg');
    

    2. 直接imcrop

    imcrop输入左下角坐标初始值,和宽度,高度即可。

    imcrop(I, [x0, y0, width, height]);

    clear all;
    clc;
    I = imread('C:\Users\Administrator\Desktop\lk.jpg');
    b = imcrop(I,[403,0,810,1080]);
    imshow(I);
    imshow(b);
    imwrite(b,'C:\Users\Administrator\Desktop\lk2.jpg','jpg');

     

    展开全文
  • 支持任意角度旋转、鼠标滑动放大缩小、双击旋转任意角度裁剪图片
  • Android调用系统, 任意比例裁剪图片

    千次阅读 2017-05-02 14:27:37
    android 裁剪图片 任意比例裁剪图片

    废话不多说,直接上代码
    核心代码:

    /**
     * 跳转到系统裁剪图片页面
     * @param imagePath 需要裁剪的图片路径
     */
    private void cropPic(String imagePath) {
        File file = new File(imagePath);
         Intent intent = new Intent("com.android.camera.action.CROP");
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
             Uri contentUri = FileProvider.getUriForFile(this, "com.leon.crop.fileprovider", file);
             intent.setDataAndType(contentUri, "image/*");
         } else {
             intent.setDataAndType(Uri.fromFile(file), "image/*");
         }
         intent.putExtra("crop", "true");
         intent.putExtra("aspectX", 0.1);
         intent.putExtra("aspectY", 0.1);
         intent.putExtra("outputX", 150);
         intent.putExtra("outputY", 150);
         intent.putExtra("return-data", true);
         intent.putExtra("scale", true);
         startActivityForResult(intent, CROP_PHOTO);
     }

    进入到裁剪页面, 可以自由选择裁剪的比例:
    图片裁剪

    代码如下:

    public class MainActivity extends AppCompatActivity {
    
        public final int CROP_PHOTO = 10;
        public final int ACTION_TAKE_PHOTO = 20;
        private ImageView ivImage;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //显示图片的imageview
            ivImage = (ImageView) findViewById(R.id.ivImage);
            //点击跳转拍照
            findViewById(R.id.btnPhoto).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    takePhoto();
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            File mPhotoFile = new File(getAppFile(this, "images/user_take.jpg"));
            switch (requestCode) {
                case CROP_PHOTO: //裁剪照片后
                    if (data != null) {
                        Bitmap bitmap = data.getExtras().getParcelable("data");
                        ivImage.setImageBitmap(bitmap);
                    }
                    //裁剪后删除拍照的照片
                    if (mPhotoFile.exists()) {
                        //noinspection ResultOfMethodCallIgnored
                        mPhotoFile.delete();
                    }
                    break;
                case ACTION_TAKE_PHOTO:
                    if (mPhotoFile.exists()) {
                        cropPic(getAppFile(this, "images/user_take.jpg"));
                    }
                    break;
            }
        }
    
        /**
         * 拍照
         */
        private void takePhoto() {
            try {
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                File file = new File(getAppFile(this, "images"));
                File mPhotoFile = new File(getAppFile(this, "images/user_take.jpg"));
                if (!file.exists()) {
                    file.mkdirs();
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri contentUri = FileProvider.getUriForFile(this, "com.pandaq.pandaeye.fileprovider", mPhotoFile);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                } else {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));
                }
                startActivityForResult(intent, ACTION_TAKE_PHOTO);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 获取本应用在系统的存储目录
         */
        public static String getAppFile(Context context, String uniqueName) {
            String cachePath;
            if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                    || !Environment.isExternalStorageRemovable())
                    && context.getExternalCacheDir() != null) {
                cachePath = context.getExternalCacheDir().getParent();
            } else {
                cachePath = context.getCacheDir().getParent();
            }
            return cachePath + File.separator + uniqueName;
        }
    
    
        /**
         * 跳转到系统裁剪图片页面
         * @param imagePath 需要裁剪的图片路径
         */
        private void cropPic(String imagePath) {
            File file = new File(imagePath);
            Intent intent = new Intent("com.android.camera.action.CROP");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Uri contentUri = FileProvider.getUriForFile(this, "com.leon.crop.fileprovider", file);
                intent.setDataAndType(contentUri, "image/*");
            } else {
                intent.setDataAndType(Uri.fromFile(file), "image/*");
            }
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 0.1);
            intent.putExtra("aspectY", 0.1);
            intent.putExtra("outputX", 150);
            intent.putExtra("outputY", 150);
            intent.putExtra("return-data", true);
            intent.putExtra("scale", true);
            startActivityForResult(intent, CROP_PHOTO);
        }
    }
    
    展开全文
  • Android 以任意比例裁剪图片

    千次阅读 2016-11-11 18:44:34
    公司的一个小伙伴写的,可以按照任意比例裁剪图片。我觉得挺好用的。简单在这里记录一下,以后肯定还会用到。 public class SeniorCropImageView extends ImageView implements ScaleGestureDetector....

    公司的一个小伙伴写的,可以按照任意比例裁剪图片。我觉得挺好用的。简单在这里记录一下,以后肯定还会用到。

    public class SeniorCropImageView extends ImageView implements ScaleGestureDetector.OnScaleGestureListener,
            View.OnLayoutChangeListener {
    
        /* For drawing color field start */
        private static final int LINE_COLOR = Color.WHITE;
        private static final int OUTER_MASK_COLOR = Color.argb(191, 0, 0, 0);
        private static final int LINE_WIDTH_IN_DP = 1;
        private final float[] mMatrixValues = new float[9];
        protected Matrix mSupportMatrix;
        protected ScaleGestureDetector mScaleGestureDetector;
        /* For drawing color field end */
        protected Paint mPaint;
        /*
         * 宽比高
         */
        protected float mRatio = 1.0f;
        protected RectF mCropRect;
        //RectFPadding是适应产品需求,给裁剪框mCropRect设置一下padding -- chenglin 2016年04月18日
        protected float RectFPadding = 0;
        protected int mLastX;
        protected int mLastY;
        protected OPERATION mOperation;
        private onBitmapLoadListener iBitmapLoading = null;
        private boolean mEnableDrawCropWidget = true;
        /*
        For scale and drag
         */
        private Matrix mBaseMatrix;
        private Matrix mDrawMatrix;
        private AccelerateDecelerateInterpolator sInterpolator = new AccelerateDecelerateInterpolator();
        private Path mPath;
        private int mLineWidth;
        private float mScaleMax = 3.0f;
        private RectF mBoundaryRect;
        private int mRotation = 0;
        private int mImageWidth;
        private int mImageHeight;
        private int mDisplayW;
        private int mDisplayH;
    
        public SeniorCropImageView(Context context) {
            this(context, null);
        }
    
        public SeniorCropImageView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public SeniorCropImageView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
    
            if (attrs != null) {
                TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Life_CropImage);
                mRatio = a.getFloat(R.styleable.Life_CropImage_life_Crop_ratio, 1.0f);
    
                a.recycle();
            }
    
            init();
        }
    
        public static void decodeImageForCropping(final String path, final IDecodeCallback callback) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int rotation = 0;
    
                    // 读取一下exif中的rotation
                    try {
                        ExifInterface exif = new ExifInterface(path);
                        final int rotate = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
                        switch (rotate) {
                            case ExifInterface.ORIENTATION_ROTATE_90:
                                rotation = 90;
                                break;
                            case ExifInterface.ORIENTATION_ROTATE_180:
                                rotation = 180;
                                break;
                            case ExifInterface.ORIENTATION_ROTATE_270:
                                rotation = 270;
                                break;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                    final BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(path, options);
    
                    final int textureLimit = getMaxTextureSize();
    
                    int scale = 1;
                    while (options.outWidth / scale >= textureLimit) {
                        scale *= 2;
                    }
    
                    while (options.outHeight / scale >= textureLimit) {
                        scale *= 2;
                    }
    
                    options.inSampleSize = scale;
                    options.inJustDecodeBounds = false;
    
                    Bitmap bitmap = null;
                    try {
                        bitmap = BitmapFactory.decodeFile(path, options);
                    } catch (OutOfMemoryError e) {
                        e.printStackTrace();
                    }
    
                    final Bitmap bimapDecoded = bitmap;
                    if (bimapDecoded == null) {
                        return;
                    }
    
                    if (callback != null) {
                        callback.onDecoded(rotation, bimapDecoded);
                    }
                }
            }).start();
    
        }
    
        private static int getMaxTextureSize() {
            EGL10 egl = (EGL10) EGLContext.getEGL();
            EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    
            // Initialise
            int[] version = new int[2];
            egl.eglInitialize(display, version);
    
            // Query total number of configurations
            int[] totalConfigurations = new int[1];
            egl.eglGetConfigs(display, null, 0, totalConfigurations);
    
            // Query actual list configurations
            EGLConfig[] configurationsList = new EGLConfig[totalConfigurations[0]];
            egl.eglGetConfigs(display, configurationsList, totalConfigurations[0], totalConfigurations);
    
            int[] textureSize = new int[1];
            int maximumTextureSize = 0;
    
            // Iterate through all the configurations to located the maximum texture size
            for (int i = 0; i < totalConfigurations[0]; i++) {
                // Only need to check for width since opengl textures are always squared
                egl.eglGetConfigAttrib(display, configurationsList[i], EGL10.EGL_MAX_PBUFFER_WIDTH, textureSize);
    
                // Keep track of the maximum texture size
                if (maximumTextureSize < textureSize[0]) {
                    maximumTextureSize = textureSize[0];
                }
            }
    
            // Release
            egl.eglTerminate(display);
    
            return maximumTextureSize;
    
        }
    
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            mDisplayW = right - left;
            mDisplayH = bottom - top;
    
            if (getDrawable() != null && ((BitmapDrawable) getDrawable()).getBitmap() != null) {
                calculateProperties(((BitmapDrawable) getDrawable()).getBitmap());
            }
        }
    
        private void init() {
    
            mScaleGestureDetector = new ScaleGestureDetector(getContext(), this);
    
            mBaseMatrix = new Matrix();
            mDrawMatrix = new Matrix();
            mSupportMatrix = new Matrix();
    
            mLineWidth = (int) dipToPixels(LINE_WIDTH_IN_DP);
            mPaint = new Paint();
    
            // 表示第一个实线段长dashOnWidth,第一个虚线段长dashOffWidth
            mPath = new Path();
    
            mCropRect = new RectF();
            mBoundaryRect = new RectF();
    
            setScaleType(ScaleType.MATRIX);
            setClickable(true);
        }
    
        private float dipToPixels(float dip) {
            return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip,
                    getResources().getDisplayMetrics());
        }
    
        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
    
            addOnLayoutChangeListener(this);
        }
    
        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
    
            removeOnLayoutChangeListener(this);
        }
    
        /**
         * 设置图片的裁剪比例,比如3:4就是0.75
         *
         * @param ratio
         */
        public void setCropRatio(final float ratio) {
            if (mRatio == ratio) {
                return;
            }
            mRatio = ratio;
    
            //重新选择比例后,恢复旋转角度
            //setImageRotation(0);
    
            if (getDrawable() == null) {
                return;
            }
    
            calculateProperties(((BitmapDrawable) getDrawable()).getBitmap());
    
            postInvalidate();
        }
    
        public void setImageRotation(int rotation) {
            if (mRotation == rotation) {
                return;
            }
    
            mRotation = rotation;
    
            if (getDrawable() == null) {
                return;
            }
    
            calculateProperties(((BitmapDrawable) getDrawable()).getBitmap());
    
            postInvalidate();
        }
    
        public void setCropRectPadding(float padding) {
            RectFPadding = padding;
        }
    
        public void setImagePath(final String path) {
            if (TextUtils.isEmpty(path)) {
                return;
            }
    
            if (iBitmapLoading != null) {
                iBitmapLoading.onLoadPrepare();
            }
            decodeImageForCropping(path, new IDecodeCallback() {
                @Override
                public void onDecoded(final int rotation, final Bitmap bitmap) {
                    post(new Runnable() {
                        @Override
                        public void run() {
                            mRotation = rotation;
                            setImageBitmap(bitmap);
    
                            if (iBitmapLoading != null) {
                                iBitmapLoading.onLoadFinish();
                            }
                        }
                    });
                }
            });
        }
    
        @Override
        public void setImageBitmap(Bitmap bm) {
            calculateProperties(bm);
            super.setImageBitmap(bm);
        }
    
        public void setBitmapLoadingListener(onBitmapLoadListener iBitmapLoad) {
            iBitmapLoading = iBitmapLoad;
        }
    
        protected void calculateProperties(Bitmap bm) {
            mSupportMatrix.reset();
            mBaseMatrix.reset();
    
            int widthSize = mDisplayW;
            int heightSize = mDisplayH;
    
            generateCropRect(widthSize, heightSize);
    
            mImageWidth = bm.getWidth();
            mImageHeight = bm.getHeight();
    
            final boolean rotated = isImageRotated();
    
            final int bitmapWidth = rotated ? mImageHeight : mImageWidth;
            final int bitmapHeight = rotated ? mImageWidth : mImageHeight;
    
            mBoundaryRect.set(0, 0, bitmapWidth, bitmapHeight);
    
            final float widthScale = mCropRect.width() / bitmapWidth;
            final float heightScale = mCropRect.height() / bitmapHeight;
    
            final float scale = Math.max(widthScale, heightScale);
            final float scaledHeight = scale * bitmapHeight;
            final float scaledWidth = scale * bitmapWidth;
    
            // 移动到中心点
            final int translateX = (int) (mCropRect.left + mCropRect.width() / 2 - scaledWidth / 2);
            final int translateY = (int) (mCropRect.top + mCropRect.height() / 2 - scaledHeight / 2);
    
            mBaseMatrix.setScale(scale, scale);
            mBaseMatrix.postTranslate(translateX, translateY);
    
            mBaseMatrix.mapRect(mBoundaryRect);
    
            setImageMatrix(getDrawMatrix());
        }
    
        private boolean isImageRotated() {
            return ((mRotation % 360) == 90) || ((mRotation % 360) == 270);
        }
    
        private void generateCropRect(int boundaryWidth, int boundaryHeight) {
            //RectFPadding是适应产品需求,给裁剪框mCropRect设置一下padding -- chenglin 2016年04月18日
            boundaryWidth = boundaryWidth - (int)(RectFPadding * 2);
            boundaryHeight = boundaryHeight - (int)(RectFPadding * 2);
    
            int left;
            int top;
            int right;
            int bottom;
    
            boolean vertical;
            // 宽/高 大于比例的话,说明裁剪框是“竖直”的
            vertical = (float) boundaryWidth / boundaryHeight > mRatio;
    
            final int rectH = (int) (boundaryWidth / mRatio);
            final int rectW = (int) (boundaryHeight * mRatio);
            if (vertical) {
                left = (boundaryWidth - rectW) / 2;
                top = 0;
                right = (boundaryWidth + rectW) / 2;
                bottom = boundaryHeight;
            } else {
                left = 0;
                top = (boundaryHeight - rectH) / 2;
                right = boundaryWidth;
                bottom = (boundaryHeight + rectH) / 2;
            }
    
            //RectFPadding是适应产品需求,给裁剪框mCropRect设置一下padding -- chenglin 2016年04月18日
            mCropRect.set(left + RectFPadding, top + RectFPadding, right + RectFPadding, bottom + RectFPadding);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            if (!mEnableDrawCropWidget) {
                return;
            }
    
            if (getDrawable() == null) {
                return;
            }
    
            mPaint.reset();
    
            mPaint.setAntiAlias(true);
            mPaint.setColor(LINE_COLOR);
            mPaint.setStrokeWidth(mLineWidth);
            mPaint.setStyle(Paint.Style.STROKE);
    
            mPath.reset();
    
            // 上
            mPath.moveTo(mCropRect.left, mCropRect.top);
            mPath.lineTo(mCropRect.right, mCropRect.top);
            // 左
            mPath.moveTo(mCropRect.left, mCropRect.top);
            mPath.lineTo(mCropRect.left, mCropRect.bottom);
            // 右
            mPath.moveTo(mCropRect.right, mCropRect.top);
            mPath.lineTo(mCropRect.right, mCropRect.bottom);
            // 下
            mPath.moveTo(mCropRect.right, mCropRect.bottom);
            mPath.lineTo(mCropRect.left, mCropRect.bottom);
    
            canvas.drawPath(mPath, mPaint);
    
            // 绘制外部阴影部分
            mPaint.reset();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.parseColor("#B3333333"));
            mPaint.setStyle(Paint.Style.FILL);
    
            //下面的四个矩形是装饰性的,就是裁剪框四周的四个阴影
            final int lineOffset = mLineWidth;
            if (mCropRect.top > 0) {
                canvas.drawRect(0, 0, getMeasuredWidth(), mCropRect.top - lineOffset, mPaint);
            }
    
            if (mCropRect.left > 0) {
                canvas.drawRect(mCropRect.top - lineOffset - RectFPadding, RectFPadding - lineOffset, mCropRect.left - lineOffset, mCropRect.bottom + lineOffset, mPaint);
            }
    
            if (mCropRect.right < getMeasuredWidth()) {
                canvas.drawRect(mCropRect.right + lineOffset, mCropRect.top - lineOffset, getMeasuredWidth(), mCropRect.bottom + lineOffset, mPaint);
            }
    
            if (mCropRect.bottom < getMeasuredHeight()) {
                canvas.drawRect(0, mCropRect.bottom + lineOffset, getMeasuredWidth(), getMeasuredHeight(), mPaint);
            }
        }
    
        public boolean onTouchEvent(MotionEvent ev) {
    
            if (ev.getPointerCount() > 1) {
                mOperation = OPERATION.SCALE;
                return mScaleGestureDetector.onTouchEvent(ev);
            }
    
            final int action = ev.getActionMasked();
            final int x = (int) ev.getX();
            final int y = (int) ev.getY();
    
            switch (action) {
                case MotionEvent.ACTION_DOWN:
    
                    mOperation = OPERATION.DRAG;
    
                    mLastX = x;
                    mLastY = y;
    
                    break;
                case MotionEvent.ACTION_MOVE:
    
                    if (mOperation == OPERATION.DRAG) {
                        int deltaX = x - mLastX;
                        int deltaY = y - mLastY;
    
                        RectF boundary = getDrawBoundary(getDrawMatrix());
    
                        if (boundary.left + deltaX > mCropRect.left) {
                            deltaX = (int) (mCropRect.left - boundary.left);
                        } else if (boundary.right + deltaX < mCropRect.right) {
                            deltaX = (int) (mCropRect.right - boundary.right);
                        }
    
                        if (boundary.top + deltaY > mCropRect.top) {
                            deltaY = (int) (mCropRect.top - boundary.top);
                        } else if (boundary.bottom + deltaY < mCropRect.bottom) {
                            deltaY = (int) (mCropRect.bottom - boundary.bottom);
                        }
    
                        mSupportMatrix.postTranslate(deltaX, deltaY);
    
                        setImageMatrix(getDrawMatrix());
    
                        mLastX = x;
                        mLastY = y;
                    }
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_POINTER_UP:
                case MotionEvent.ACTION_UP:
                    mLastX = 0;
                    mLastY = 0;
                    mOperation = null;
                    break;
            }
    
            return super.onTouchEvent(ev);
        }
    
        public Bitmap getOriginBitmap() {
            BitmapDrawable drawable = (BitmapDrawable) getDrawable();
            return drawable == null ? null : drawable.getBitmap();
        }
    
        /**
         * 保存图片为bitmap
         */
        public Bitmap saveCrop() throws OutOfMemoryError {
            if (getDrawable() == null) {
                return null;
            }
    
            Bitmap origin = getOriginBitmap();
            Matrix drawMatrix = getDrawMatrix();
    
            // 反转一下矩阵
            Matrix inverse = new Matrix();
            drawMatrix.invert(inverse);
    
            // 把裁剪框对应到原图上去
            RectF cropMapped = new RectF();
            inverse.mapRect(cropMapped, mCropRect);
    
            clampCropRect(cropMapped, origin.getWidth(), origin.getHeight());
    
            // 如果产生了旋转,需要一个旋转矩阵
            Matrix rotationM = new Matrix();
            if (mRotation % 360 != 0) {
                rotationM.postRotate(mRotation, origin.getWidth() / 2, origin.getHeight() / 2);
            }
    
            Bitmap cropped = Bitmap.createBitmap(
                    origin, (int) cropMapped.left, (int) cropMapped.top, (int) cropMapped.width(), (int) cropMapped.height(), rotationM, true
            );
    
    
            return cropped;
        }
    
        private void clampCropRect(RectF cropRect, int borderW, int borderH) {
            if (cropRect.left < 0) {
                cropRect.left = 0;
            }
    
            if (cropRect.top < 0) {
                cropRect.top = 0;
            }
    
            if (cropRect.right > borderW) {
                cropRect.right = borderW;
            }
    
            if (cropRect.bottom > borderH) {
                cropRect.bottom = borderH;
            }
        }
    
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            float scale = detector.getScaleFactor();
    
            if (scale == 1.0f) {
                return true;
            }
    
            final float currentScale = getScale(mSupportMatrix);
    
            final float centerX = detector.getFocusX();
            final float centerY = detector.getFocusY();
    
            if ((currentScale <= 1.0f && scale < 1.0f)
                    || (currentScale >= mScaleMax && scale > 1.0f)) {
                return true;
            }
    
            if (currentScale * scale < 1.0f) {
                scale = 1.0f / currentScale;
            } else if (currentScale * scale > mScaleMax) {
                scale = mScaleMax / currentScale;
            }
    
            mSupportMatrix.postScale(scale, scale, centerX, centerY);
    
            RectF boundary = getDrawBoundary(getDrawMatrix());
    
            float translateX = 0;
            if (boundary.left > mCropRect.left) {
                translateX = mCropRect.left - boundary.left;
            } else if (boundary.right < mCropRect.right) {
                translateX = mCropRect.right - boundary.right;
            }
    
            Log.d("scale", "x==>" + translateX);
    
            float translateY = 0;
            if (boundary.top > mCropRect.top) {
                translateY = mCropRect.top - boundary.top;
            } else if (boundary.bottom < mCropRect.bottom) {
                translateY = mCropRect.bottom - boundary.bottom;
            }
    
            mSupportMatrix.postTranslate(translateX, translateY);
    
            setImageMatrix(getDrawMatrix());
    
            return true;
        }
    
        protected Matrix getDrawMatrix() {
            mDrawMatrix.reset();
    
            if (mRotation % 360 != 0) {
                final boolean rotated = isImageRotated();
    
                final int width = rotated ? mImageHeight : mImageWidth;
                final int height = rotated ? mImageWidth : mImageHeight;
    
                mDrawMatrix.postRotate(mRotation, mImageWidth / 2, mImageHeight / 2);
    
                if (rotated) {
                    final int translateX = (width - mImageWidth) / 2;
                    final int translateY = (height - mImageHeight) / 2;
    
                    mDrawMatrix.postTranslate(translateX, translateY);
                }
            }
    
            mDrawMatrix.postConcat(mBaseMatrix);
    
            mDrawMatrix.postConcat(mSupportMatrix);
    
            return mDrawMatrix;
        }
    
        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            return true;
        }
    
        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            final float currentScale = getScale(mSupportMatrix);
            if (currentScale < 1.0f) {
                Log.e("onScaleEnd", "currentScale==>" + currentScale);
    
                RectF boundary = getDrawBoundary(getDrawMatrix());
                post(new AnimatedZoomRunnable(currentScale, 1.0f, boundary.centerX(), boundary.centerY()));
            }
        }
    
        protected RectF getDrawBoundary(Matrix matrix) {
            Drawable drawable = getDrawable();
            if (drawable == null) {
                return mBoundaryRect;
            }
    
            final int bitmapWidth = drawable.getIntrinsicWidth();
            final int bitmapHeight = drawable.getIntrinsicHeight();
    
            mBoundaryRect.set(0, 0, bitmapWidth, bitmapHeight);
    
            matrix.mapRect(mBoundaryRect);
    
            return mBoundaryRect;
        }
    
        public float getScale(Matrix matrix) {
            return (float) Math.sqrt((float) Math.pow(getValue(matrix, Matrix.MSCALE_X), 2) + (float) Math.pow(getValue(matrix, Matrix.MSKEW_Y), 2));
        }
    
        /**
         * Helper method that 'unpacks' a Matrix and returns the required value
         *
         * @param matrix     - Matrix to unpack
         * @param whichValue - Which value from Matrix.M* to return
         * @return float - returned value
         */
        private float getValue(Matrix matrix, int whichValue) {
            matrix.getValues(mMatrixValues);
            return mMatrixValues[whichValue];
        }
    
        public void enableDrawCropWidget(boolean enable) {
            mEnableDrawCropWidget = enable;
        }
    
        protected enum OPERATION {
            DRAG, SCALE
        }
    
        public enum Type {
            CENTER_CROP, CENTER_INSIDE
        }
    
        public interface IDecodeCallback {
            void onDecoded(final int rotation, final Bitmap bitmap);
        }
    
        //setImagePath这个方法耗时,需要显示进度条,这个是监听
        public interface onBitmapLoadListener {
            void onLoadPrepare();
    
            void onLoadFinish();
        }
    
        private class AnimatedZoomRunnable implements Runnable {
    
            private final float mFocalX, mFocalY;
            private final long mStartTime;
            private final float mZoomStart, mZoomEnd;
    
            public AnimatedZoomRunnable(final float currentZoom, final float targetZoom,
                                        final float focalX, final float focalY) {
                mFocalX = focalX;
                mFocalY = focalY;
                mStartTime = System.currentTimeMillis();
                mZoomStart = currentZoom;
                mZoomEnd = targetZoom;
            }
    
            @Override
            public void run() {
    
                float t = interpolate();
                float scale = mZoomStart + t * (mZoomEnd - mZoomStart);
                float deltaScale = scale / getScale(mSupportMatrix);
    
                mSupportMatrix.postScale(deltaScale, deltaScale, mFocalX, mFocalY);
                setImageMatrix(getDrawMatrix());
    
                // We haven't hit our target scale yet, so post ourselves again
                if (t < 1f) {
                    postOnAnimation(this);
                }
            }
    
            private float interpolate() {
                float t = 1f * (System.currentTimeMillis() - mStartTime) / 200;
                t = Math.min(1f, t);
                t = sInterpolator.getInterpolation(t);
                return t;
            }
        }
    
    }


        <declare-styleable name="Life_CropImage">
            <attr name="life_Crop_ratio" format="float" />
            <attr name="life_Crop_scale_type" format="enum">
                <enum name="life_center_crop" value="0" />
                <enum name="life_center_inside" value="1" />
            </attr>
        </declare-styleable>

    1、让这个裁剪框显示图片:

    mSeniorImageView.setImagePath(path);
    2、保存裁剪后的图片:

            Bitmap imageViewBitmap = null;
            try {
                imageViewBitmap = mSeniorImageView.saveCrop();
            } catch (OutOfMemoryError e) {
                imageViewBitmap = mSeniorImageView.getOriginBitmap();
                PinkToast.makeText(mActivity, R.string.life_image_crop_topbar_crop_error, Toast.LENGTH_LONG).show();
            }

    3、设置裁剪比例:

    mSeniorImageView.setCropRatio(3f / 4f);
    4、设置裁剪框的padding:
    mSeniorImageView.setCropRectPadding(0f);
    5、setImagePath这个方法比较耗时,需要显示进度条,这个是监听:

            mSeniorImageView.setBitmapLoadingListener(new SeniorCropImageView.onBitmapLoadListener() {
                @Override
                public void onLoadPrepare() {
                    mActivity.showProgress();
                }
    
                @Override
                public void onLoadFinish() {
                    mActivity.hideProgress();
                }
    
            });
    

    ===============================

    如果你觉得帮到了你,请给作者打赏一口饭吃:


    展开全文
  • SimpleCrop 目前是 全网唯一 支持裁剪图片任意角度旋转、交互体验 媲美原生客户端 的 全平台 图片裁剪组件。 项目地址: github.com/newbieYoung… 。 特性及优势 和目前流行的图片裁剪组件相比,其优势...
  • 摘要:VC/C++源码,图形处理,裁剪图片 VC++裁剪任意图片为椭圆形,不过只支持BMP格式的位图,对于截图的原理,请自行分析吧。
  • iOS 图片裁剪功能。

    2017-10-30 16:58:56
    图片裁剪功能。包括图片缩放、旋转、裁剪任意比例后裁剪。 示例效果: Demo在github上: https://github.com/MiftMy/test
  • 本程序可以任意形状裁剪照片,但是裁剪后有背景色。
  • VB裁剪图片

    热门讨论 2013-06-14 12:13:47
    VB编的可以裁剪任意图片大小的程序,可以很方便根据选择裁剪图片
  • 今天小编就为大家分享一篇python随机在一张图像上截取任意大小图片的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
  • PS裁剪图片任意形状区域

    千次阅读 2019-12-13 11:16:04
    使用套索工具 用套索工具随便画一个形状; 然后反选(ctrl+shift+i) 使用删除键,删除即可。
  • 裁剪图片的指定位置

    千次阅读 2016-06-29 19:28:33
    // - UIImage 的类别的方法 - (UIImage *)getImageWithRect:(CGRect)rect{      CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);... UIImage *thumbScale = [UIImage
  • Mac app 中图片任意的旋转和裁剪例子
  • 源码YQImageEditor,iOS 系统...所以自己写了个照片编辑器,暂时只实现了按任意宽高比裁剪图片。也提供选取圆形图片,但裁剪出来依旧是正方形。利用mask显示成圆形即可。按此思路,稍加扩展,即可实现任意形状的裁剪。
  • unity3d任意图片截取和裁剪

    万次阅读 2014-01-23 19:53:25
    楼主遇到打包获取的图片去截取一部分去贴图,这个问题令人费解,刚开始我想出的办法是采集像素点去拼接成这个截取的部分,可是当做完后在楼主愉快的心情去运行到ipad上发现,卡巴斯基了(卡死了),其实想想也是这种...
  • 今天小编就为大家分享一篇python opencv对图像进行旋转且不裁剪图片的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
  • Android 自定义裁剪图片

    热门讨论 2012-12-17 17:51:01
    Android 自己 写代码 实现 图片裁剪 解决了系统裁剪的不少问题。 可以下来看看。
  • NULL 博文链接:https://wujuncheng.iteye.com/blog/456413
  • 图片任意位置裁剪

    千次阅读 2017-02-23 13:33:32
    图片任意位置裁剪调用只需一步传入要裁剪图片位置和图片大小即可https://github.com/hanxinhui/CutImageAnywhere.git
  • 使用cropper.js裁剪图片,通过canvas获取裁剪后的图片,获取的是base64图片上传。
  • 该资源是自己做的(后台是java,页面是jsp,用了Jcrop插件裁剪图片),能实现上传本地图片,预览在页面上并根据用户的需求进行任意大小的裁剪,最后将裁剪后的图片进行展示。整个过程会把图片的名字插入数据库字段;...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 14,018
精华内容 5,607
关键字:

任意裁剪图片