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
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");
postez votre code. –
poster votre code, pourrait être à cause de thème/fond –
@NabinBhandari J'ai édité le problème avec mon code –