繁体   English   中英

如何从画廊或相机像Android中的whatapp裁剪图像

[英]how to crop image from gallery or camera like whatapp in android

裁剪图像,例如Whats App个人资料照片

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
        intent.setType("image/*");
         select = "image";
         intent.setType("image/*");
         intent.putExtra("crop", "true");
         intent.putExtra("aspectX", width+20);
         intent.putExtra("aspectY", 170);
         intent.putExtra("outputX", width+20);
         intent.putExtra("outputY", 150);
         intent.putExtra("return-data", true);
   startActivityForResult(Intent.createChooser(intent,"Complete action using"), 111);


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)

            if(requestCode ==111)
            {
            Bitmap photo2 = null;
                Bundle extras2 = data.getExtras();
                if (extras2 != null) {
                photo2 = extras2.getParcelable("data");
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                photo2.compress(Bitmap.CompressFormat.PNG, 100,byteArrayOutputStream);
                byte[] byteArray = byteArrayOutputStream.toByteArray();
                encoded = Base64.encodeToString(byteArray,Base64.DEFAULT);
                Log.e("", "bitmap"+encoded);
                Drawable d = new BitmapDrawable(getResources(),photo2);
                imgprofile.setBackgroundDrawable(d);


            }

        }

使用图像裁剪库

您的附加功能无法在大多数设备上使用,因为它们没有文件记录且通常不受支持。 不需要每个ACTION_GET_CONTENT活动都可以实现图像裁剪。

如果您想使用裁剪裁剪图像,则可以使用此类:

public class RoundedImageView extends ImageView {
    private Paint objPaint = new Paint();

    public RoundedImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public RoundedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        Drawable drawable = getDrawable();

        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        Bitmap bitmap = b.copy(Config.ARGB_8888, true);

        int w = getWidth(), h = getHeight();

        Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
        objPaint.setAntiAlias(true);
        objPaint.setDither(true);
        canvas.drawBitmap(roundBitmap, 0, 0, objPaint);

    }

    public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
        Bitmap sbmp;
        if (bmp.getWidth() != radius || bmp.getHeight() != radius)
            sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
        else
            sbmp = bmp;
        Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),
                Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xffa19774;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#BAB399"));
        canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,
                sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(sbmp, rect, rect, paint);

        return output;
    }

}

在xml中调用此类:

<com.RoundedImageView
  android:id="@+id/ivCardLogoFourLogo"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#00000000"
  android:gravity="center"
  android:padding="4dp" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM