2016-03-09 47 views
0

裁剪图像像什么应用配置文件PIC如何从画廊或相机像whatapp裁剪图像中的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); 


      } 

     } 

回答

0

Use an image-cropping library

您的演员在大多数设备上无法使用,因为他们通常没有记录且不受支持。没有要求每个ACTION_GET_CONTENT活动实施图像裁剪。

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