如何从画廊或相机中剪切图像像android中的whatapp

时间:2016-03-09 12:41:25

标签: android

裁剪图片,如应用资料图片

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);


            }

        }

2 个答案:

答案 0 :(得分:0)

Use an image-cropping library

您的附加内容不适用于大多数设备,因为它们没有文档记录且一般不受支持。每个ACTION_GET_CONTENT活动都不需要实现图像裁剪。

答案 1 :(得分:0)

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

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" />