2017-09-30 7 views
0

J'essaye de redimensionner un bitmap dans Android mais le résultat a toujours des bordures noires.Redimensionner un bitmap dans Android provoque des bordures noires

enter image description here

On m'a déjà dit que cela a été causé parce que je ne pas utiliser la largeur et la hauteur exactes. La méthode createScaledBitmap autorise uniquement les ints. J'ai essayé de contourner ce problème, mais même lors de la conversion de floats en ints, le problème persiste et je ne remarque aucune différence.

Je maintiens le rapport d'aspect.

Est-ce que quelqu'un a un exemple de travail?

code:

      int dstWidth = (int)(srcWidth*0.15f); 

          BitmapFactory.Options options = new BitmapFactory.Options(); 
          options.inScaled = false; 
          Bitmap newBitmap = getScaledDownBitmap(orientedBitmap, dstWidth, false); 

La méthode sa vocation:

public static Bitmap getScaledDownBitmap(Bitmap bitmap, int threshold, boolean isNecessaryToKeepOrig){ 
    int width = bitmap.getWidth(); 
    int height = bitmap.getHeight(); 
    int newWidth = width; 
    int newHeight = height; 

    if(width > height && width > threshold){ 
     newWidth = threshold; 
     newHeight = (int)(height * (float)newWidth/width); 
    } 

    if(width > height && width <= threshold){ 
     //the bitmap is already smaller than our required dimension, no need to resize it 
     return bitmap; 
    } 

    if(width < height && height > threshold){ 
     newHeight = threshold; 
     newWidth = (int)(width * (float)newHeight/height); 
    } 

    if(width < height && height <= threshold){ 
     //the bitmap is already smaller than our required dimension, no need to resize it 
     return bitmap; 
    } 

    if(width == height && width > threshold){ 
     newWidth = threshold; 
     newHeight = newWidth; 
    } 

    if(width == height && width <= threshold){ 
     //the bitmap is already smaller than our required dimension, no need to resize it 
     return bitmap; 
    } 

    return getResizedBitmap(bitmap, newWidth, newHeight, isNecessaryToKeepOrig); 
} 

Juste pour que vous le savez, la même chose se produit avec createScaledBitmap. J'ai essayé plusieurs images.

Qu'est-ce que le code ressemble, en utilisant le code de Marzieh Heidari (n'a pas résolu le problème)

      File imagefile = new File(filePath); 
         FileInputStream fis = null; 
         try{ 
          fis = new FileInputStream(imagefile); 
         }catch(FileNotFoundException e){ 
          e.printStackTrace(); 
         } 

         Bitmap bm = BitmapFactory.decodeStream(fis); 
         Bitmap orientedBitmap = ExifUtil.rotateBitmap(filePath, bm); 

         ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
         if(orientedBitmap.getHeight() >= 600 && orientedBitmap.getWidth() >= 600) { 
          int srcWidth = orientedBitmap.getWidth(); 
          int srcHeight = orientedBitmap.getHeight(); 
          int dstWidth = (int)(srcWidth*0.15f); 
          int dstHeight = (int)(srcHeight*0.15f); 

          BitmapFactory.Options options = new BitmapFactory.Options(); 
          options.inScaled = false; 
          Bitmap newBitmap = createScaledBitmap(orientedBitmap, dstWidth, dstHeight, ScalingLogic.CROP); 
          newBitmap.compress(Bitmap.CompressFormat.JPEG,60,baos); 
          byte[] b = baos.toByteArray(); 
          encImage = Base64.encodeToString(b, Base64.DEFAULT); 
         } else { 
          orientedBitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos); 
          byte[] b = baos.toByteArray(); 
          encImage = Base64.encodeToString(b, Base64.DEFAULT); 
         } 

De là, je posterai essentiellement le base64 à PHP

$base = $_POST['base']; 
$imageData = base64_decode($base); 
$source = imagecreatefromstring($imageData); 

$imageName = 'avatars/1.jpeg'; 
$imageSave = imagejpeg($imageName,100); 
imagedestroy($source); 

XML

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/image_border_blue"> 
<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="58dp" 
    android:layout_height="58dp" 
    android:clickable="true" 
    android:scaleType="centerCrop" 
    android:onClick="StartImage" 
    android:src="@drawable/user_male_icon" /> 
</RelativeLayout> 

user_male_icon est juste un défaut, les images sont chargées en utilisant Ion:

 Ion.with(nav_image) 
      .placeholder(R.drawable.user_male_icon) 
      .load(Settings.WEBSITE_ADDR + "/-------/avatars/1.jpeg"); 
+1

postez votre code. –

+0

poster votre code, pourrait être à cause de thème/fond –

+0

@NabinBhandari J'ai édité le problème avec mon code –

Répondre

0

C'est ce que j'utilise pour mise à l'échelle bitmap. espérons que cela aide

/** 
* Utility function for creating a scaled version of an existing bitmap 
* 
* @param unscaledBitmap Bitmap to scale 
* @param dstWidth  Wanted width of destination bitmap 
* @param dstHeight  Wanted height of destination bitmap 
* @param scalingLogic Logic to use to avoid image stretching 
* @return New scaled bitmap object 
*/ 
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight, 
             ScalingLogic scalingLogic) { 
    Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), 
      dstWidth, dstHeight, scalingLogic); 
    Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), 
      dstWidth, dstHeight, scalingLogic); 
    Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), 
      Config.ARGB_8888); 
    Canvas canvas = new Canvas(scaledBitmap); 
    canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG)); 

    return scaledBitmap; 
} 

/** 
* Calculates source rectangle for scaling bitmap 
* 
* @param srcWidth  Width of source image 
* @param srcHeight Height of source image 
* @param dstWidth  Width of destination area 
* @param dstHeight Height of destination area 
* @param scalingLogic Logic to use to avoid image stretching 
* @return Optimal source rectangle 
*/ 


    private static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, 
             ScalingLogic scalingLogic) { 
     if (scalingLogic == ScalingLogic.CROP) { 
      float srcAspect = (float) srcWidth/(float) srcHeight; 
      float dstAspect = (float) dstWidth/(float) dstHeight; 

      if (srcAspect > dstAspect) { 
       int srcRectWidth = (int) (srcHeight * dstAspect); 
       int srcRectLeft = (srcWidth - srcRectWidth)/2; 
       return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight); 
      } else { 
       int srcRectHeight = (int) (srcWidth/dstAspect); 
       int scrRectTop = (srcHeight - srcRectHeight)/2; 
       return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight); 
      } 
     } else { 
      return new Rect(0, 0, srcWidth, srcHeight); 
     } 
    } 

    /** 
    * Calculates destination rectangle for scaling bitmap 
    * 
    * @param srcWidth  Width of source image 
    * @param srcHeight Height of source image 
    * @param dstWidth  Width of destination area 
    * @param dstHeight Height of destination area 
    * @param scalingLogic Logic to use to avoid image stretching 
    * @return Optimal destination rectangle 
    */ 
    private static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, 
             ScalingLogic scalingLogic) { 
     if (scalingLogic == ScalingLogic.FIT) { 
      float srcAspect = (float) srcWidth/(float) srcHeight; 
      float dstAspect = (float) dstWidth/(float) dstHeight; 

      if (srcAspect > dstAspect) { 
       return new Rect(0, 0, dstWidth, (int) (dstWidth/srcAspect)); 
      } else { 
       return new Rect(0, 0, (int) (dstHeight * srcAspect), dstHeight); 
      } 
     } else { 
      return new Rect(0, 0, dstWidth, dstHeight); 
     } 
    } 
/** 
    * ScalingLogic defines how scaling should be carried out if source and 
    * destination image has different aspect ratio. 
    * <p> 
    * CROP: Scales the image the minimum amount while making sure that at least 
    * one of the two dimensions fit inside the requested destination area. 
    * Parts of the source image will be cropped to realize this. 
    * <p> 
    * FIT: Scales the image the minimum amount while making sure both 
    * dimensions fit inside the requested destination area. The resulting 
    * destination dimensions might be adjusted to a smaller size than 
    * requested. 
    */ 
    public enum ScalingLogic { 
     CROP, FIT 
    } 

MISE À JOUR: Vous avez mentionné que vous avez besoin de quoi rogner image.I espérons que cette aide individuelle

Bitmap croppedBmp = Bitmap.createBitmap(originalBmp, rectanglePositionX, rectanglePositionY, rectangleWidth, rectangleHeight); 

rectanglePositionX et rectanglePositionY doit être la largeur des frontières, rectangleWidth shuold être la largeur bitmep - borderWidth et enfin rectangleHeight devrait être bitmep height- borderWidth

+0

Qu'est-ce que ScalingLogic? –

+0

@Fer C'est une énumération que j'ai oublié d'ajouter.J'ai ajouté à la fin du code maintenant –

+0

Cela n'a pas fonctionné mais s'il vous plaît regardez le poste. Je l'ai édité avec le code que j'utilise avant et après avoir exécuté les méthodes que vous m'avez données –

0

Ceci est un cas étrange. Essayez une des options suivantes:

  • Utilisez scaleType fitCenter
  • Désactiver l'accélération matérielle
  • Test du code dans un autre appareil.
+0

Rien n'a aidé malheureusement. c'est l'image réelle avec les bordures noires, donc ça n'a rien à voir avec l'imageview.Bien sur s'il y a un moyen de zoomer 3px de chaque côté, ça enlèverait les bordures noires mais je ne sais pas comment –