2013-03-04 2 views

ответ

1

Попробуйте с этим ... подрезать в форме человеческого лица

Uri ImageCaptureUri = Uri.fromFile(new File("filepath"); 
Intent intent = new Intent("com.android.camera.action.CROP"); 
intent.setType("image/*"); 
intent.setData(ImageCaptureUri); 
intent.putExtra("outputX", 200); 
intent.putExtra("outputY", 200); 
intent.putExtra("aspectX", 1); 
intent.putExtra("aspectY", 1); 
intent.putExtra("scale", true); 
intent.putExtra("return-data", true); 
startActivityForResult(intent, 1); 
+0

он просит com.android.gallery класса – amarnathreddy

+0

intent.setComponent (новый ComponentName ("com.android. камера "," com.android.camera.CropImage ")); – Jambaaz

+0

если вы не возражаете, пожалуйста, поделитесь общим кодом пользователя. – amarnathreddy

2

Исследуйте com.android.camera.CropImage.java sources. Он может обрезать круглые изображения.

// if we're circle cropping we'll want alpha which is the third param here 
    464 mCroppedImage = Bitmap.createBitmap(width, height, 
    465     mCircleCrop ? 
    466       Bitmap.Config.ARGB_8888 : 
    467       Bitmap.Config.RGB_565); 
    468 Canvas c1 = new Canvas(mCroppedImage); 
    469 c1.drawBitmap(mBitmap, r, new Rect(0, 0, width, height), null); 
    470 
    471 if (mCircleCrop) { 
    472  // OK, so what's all this about? 
    473  // Bitmaps are inherently rectangular but we want to return something 
    474  // that's basically a circle. So we fill in the area around the circle 
    475  // with alpha. Note the all important PortDuff.Mode.CLEAR. 
    476  Canvas c = new Canvas (mCroppedImage); 
    477  android.graphics.Path p = new android.graphics.Path(); 
    478  p.addCircle(width/2F, height/2F, width/2F, android.graphics.Path.Direction.CW); 
    479  c.clipPath(p, Region.Op.DIFFERENCE); 
    480 
    481  fillCanvas(width, height, c); 
    482 } 
4

Я использовал следующее в одном из своих проектов. Может быть, это поможет вам.

public Drawable getRoundedCornerImage(Drawable bitmapDrawable) { 
     Bitmap bitmap = ((BitmapDrawable)bitmapDrawable).getBitmap(); 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 
       bitmap.getHeight(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final int color = 0xff424242; 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     final RectF rectF = new RectF(rect); 
     final float roundPx = 10; 

     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(color); 
     canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 
     Drawable image = new BitmapDrawable(output); 
     return image; 

    } 
6

Я использовал следующий метод и передал свой захваченный растровый образ этому методу. И это сработает.

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) { 
     int targetWidth = 125; 
     int targetHeight = 125; 

     Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
       targetHeight, Bitmap.Config.ARGB_8888); 

     Canvas canvas = new Canvas(targetBitmap); 
     Path path = new Path(); 
     path.addCircle(
       ((float) targetWidth - 1)/2, 
       ((float) targetHeight - 1)/2, 
       (Math.min(((float) targetWidth), ((float) targetHeight))/2), 
       Path.Direction.CCW); 

     canvas.clipPath(path); 
     Bitmap sourceBitmap = scaleBitmapImage; 
     canvas.drawBitmap(
       sourceBitmap, 
       new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap 
         .getHeight()), new Rect(0, 0, targetWidth, 
         targetHeight), p); 
     return targetBitmap; 
    } 

И выход заключается в следующем: - Image

2

@vokilam ты прав; Я только исследовал в код и нашел способ обойти ...

Просто включите эту строку в основной деятельности
intent.putExtra(CropImage.CIRCLE_CROP, "circleCrop");

Но вы получите только круги, а не овальные; так что @amarnathreddy вы не можете вырезать идеальное человеческое лицо с этим; вместо того, чтобы идти на Grabcut of OpenCv

0

Для овальной формы попробовать эту функцию андроида или download demo here

enter image description here

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

     Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), 
       finalBitmap.getHeight()); 

     paint.setAntiAlias(true); 
     paint.setFilterBitmap(true); 
     paint.setDither(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(Color.parseColor("#BAB399")); 
     RectF oval = new RectF(0, 0, 130, 150); 
     canvas.drawOval(oval, paint); 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
     canvas.drawBitmap(finalBitmap, rect, oval, paint); 

     return output; 
    } 

выше функция создает равномерную овальную форму в андроиде программно. обратитесь к функции OnCreate функции и передать изображение, чтобы обрезать овальную форму в виде растрового изображения

Read more

Смежные вопросы