Android Bitmap details

  • 2020-05-07 20:22:13
  • OfStack

 
package com.testbitmapscale; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Iterator; 
import com.testbitmapscale.R.drawable; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.Config; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Matrix; 
import android.graphics.Paint; 
import android.graphics.PorterDuff.Mode; 
import android.graphics.PorterDuffXfermode; 
import android.graphics.Rect; 
import android.graphics.RectF; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.media.ThumbnailUtils; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.View; 
import android.widget.ImageView; 
// methods : 
//1  To generate rounded corners Bitmap The picture  
//2  generate Bitmap Drop-off figure  
//3  Compressed image field length and width as well kB 
// Pay attention to : 
// The above code , The test of 1 It is best to comment out the rest of the code when writing a method  
public class MainActivity extends Activity { 
private ImageView imageView; 
private Bitmap copyRawBitmap1; 
private Bitmap copyRawBitmap2; 
private Bitmap copyRawBitmap3; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
imageView = (ImageView) findViewById(R.id.imageView); 
// The first 1 Kind of way : Get the picture from the resource file  
Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.haha); 
copyRawBitmap1=rawBitmap; 
copyRawBitmap2=rawBitmap; 
copyRawBitmap3=rawBitmap; 
// The first 2 Kind of way : from SD Get the picture in the card ( methods 1) 
String SDCarePath=Environment.getExternalStorageDirectory().toString(); 
String filePath=SDCarePath+"/"+"haha.jpg"; 
Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null); 
// The first 2 Kind of way : from SD Get the picture in the card ( methods 2) 
InputStream inputStream=getBitmapInputStreamFromSDCard("haha.jpg"); 
Bitmap rawBitmap2 = BitmapFactory.decodeStream(inputStream); 

// -- -- -- > The following is the rounded corner of the image to be set  
Bitmap roundCornerBitmap=this.toRoundCorner(rawBitmap, 40); 
imageView.setImageBitmap(roundCornerBitmap); 
// -- -- -- > Above will set the rounded corners of the picture  

// -- -- -- > The following is the size and height of the image kB The compression  
//  Get the original height and width of the picture  
int rawHeight = rawBitmap.getHeight(); 
int rawWidth = rawBitmap.getWidth(); 
//  Set the new height and width of the image  
int newHeight = 500; 
int newWidth = 500; 
//  Calculate the scaling factor  
float heightScale = ((float) newHeight) / rawHeight; 
float widthScale = ((float) newWidth) / rawWidth; 
//  Newly constructed matrix  
Matrix matrix = new Matrix(); 
matrix.postScale(heightScale, widthScale); 
//  Set the rotation Angle of the image  
//matrix.postRotate(-30); 
//  Set the tilt of the image  
//matrix.postSkew(0.1f, 0.1f); 
// Compress the image size  
// The width and height of the compressed image as well kB It varies in size  
Bitmap newBitmap = Bitmap.createBitmap(rawBitmap, 0, 0, rawWidth,rawWidth, matrix, true); 
//  will Bitmap convert Drawable 
Drawable newBitmapDrawable = new BitmapDrawable(newBitmap); 
imageView.setImageDrawable(newBitmapDrawable); 
// then Bitmap Save to SDCard In the , Facilitate the comparison of original pictures  
this.compressAndSaveBitmapToSDCard(newBitmap, "xx100.jpg", 80); 
// The problem : 
// The original size is 625x690 90.2kB 
// If I set the picture 500x500  The compressed size is 171kB. The compressed kB It gets bigger . 
// The reason is the :compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream); 
// The first 2 A parameter quality It's a little too big ( Such as 100). 
// Commonly used are 80, Just set 100 It's too big . 
// -- -- -- > Above is the height, width and size of the picture kB The compression  


// -- -- -- > The following is the picture kB The compression , Wide high constant  
this.compressAndSaveBitmapToSDCard(copyRawBitmap1,"0011fa.jpg",80); 
// -- -- -- > The above is the picture kB The compression , Wide high constant  

// -- -- -- > The following is obtained SD Card picture thumbnail method 1 
String SDCarePath1=Environment.getExternalStorageDirectory().toString(); 
String filePath1=SDCarePath1+"/"+"haha.jpg"; 
Bitmap bitmapThumbnail1=this.getBitmapThumbnail(filePath1); 
imageView.setImageBitmap(bitmapThumbnail1); 
// -- -- -- > Above is acquisition SD Card picture thumbnail method 1 

// -- -- -- > The following is obtained SD Card picture thumbnail method 2 
String SDCarePath2=Environment.getExternalStorageDirectory().toString(); 
String filePath2=SDCarePath2+"/"+"haha.jpg"; 
Bitmap tempBitmap=BitmapFactory.decodeFile(filePath2); 
Bitmap bitmapThumbnail2=ThumbnailUtils.extractThumbnail(tempBitmap, 100, 100); 
imageView.setImageBitmap(bitmapThumbnail2); 
// -- -- -- > Above is acquisition SD Card picture thumbnail method 2 

} 
// read SD The picture under the card  
private InputStream getBitmapInputStreamFromSDCard(String fileName){ 
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
String SDCarePath=Environment.getExternalStorageDirectory().toString(); 
String filePath=SDCarePath+File.separator+fileName; 
File file=new File(filePath); 
try { 
FileInputStream fileInputStream=new FileInputStream(file); 
return fileInputStream; 
} catch (Exception e) { 
e.printStackTrace(); 
} 

} 
return null; 
} 


// To obtain SDCard Directory path function  
private String getSDCardPath() { 
String SDCardPath = null; 
//  judge SDCard If there is a  
boolean IsSDcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
if (IsSDcardExist) { 
SDCardPath = Environment.getExternalStorageDirectory().toString(); 
} 
return SDCardPath; 
} 
// Compress and save the image to SDCard 
private void compressAndSaveBitmapToSDCard(Bitmap rawBitmap,String fileName,int quality){ 
String saveFilePaht=this.getSDCardPath()+File.separator+fileName; 
File saveFile=new File(saveFilePaht); 
if (!saveFile.exists()) { 
try { 
saveFile.createNewFile(); 
FileOutputStream fileOutputStream=new FileOutputStream(saveFile); 
if (fileOutputStream!=null) { 
//imageBitmap.compress(format, quality, stream); 
// Writes bitmap compression information to 1 In the specified output stream  
// The first 1 A parameter format For compressed format  
// The first 2 A parameter quality Is the value of the image compression ratio ,0-100.0  It means small size compression ,100 That means high quality compression  
// The first 3 A parameter stream For the output stream  
rawBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream); 
} 
fileOutputStream.flush(); 
fileOutputStream.close(); 
} catch (IOException e) { 
e.printStackTrace(); 

} 
} 
} 

// Gets a thumbnail of the image  
private Bitmap getBitmapThumbnail(String filePath){ 
BitmapFactory.Options options=new BitmapFactory.Options(); 
//true Then the actual will not be returned bitmap object , No memory space is allocated but it is available 1 Some decoded boundary information is the size of the picture and so on  
options.inJustDecodeBounds=true; 
// At this time rawBitmap for null 
Bitmap rawBitmap = BitmapFactory.decodeFile(filePath, options); 
if (rawBitmap==null) { 
System.out.println(" At this time rawBitmap for null"); 
} 
//inSampleSize Indicates that the thumbnail size is a fraction of the original image size 1, If the value is 3 
// The width and height of the thumbnail taken out are the original image 1/3, The image size is the original size 1/9 
// To calculate sampleSize 
int sampleSize=computeSampleSize(options, 150, 200*200); 
// To read the picture , Must take options.inJustDecodeBounds Set back false 
options.inJustDecodeBounds = false; 
options.inSampleSize = sampleSize; 
// The original size is 625x690 90.2kB 
// Test call computeSampleSize(options, 100, 200*100); 
// get sampleSize=8 
// Get wide and high 79 and 87 
//79*8=632 87*8=696 
Bitmap thumbnailBitmap=BitmapFactory.decodeFile(filePath, options); 
// Save to SD Card convenient comparison  
this.compressAndSaveBitmapToSDCard(thumbnailBitmap, "15.jpg", 80); 
return thumbnailBitmap; 
} 

// Reference:  
//http://my.csdn.net/zljk000/code/detail/18212 
// The first 1 A parameter : originally Bitmap the options 
// The first 2 A parameter : A small value of the width and height of the thumbnail that you want to generate  
// The first 3 A parameter : The total number of pixels of the desired reduction graph  
public static int computeSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) { 
int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels); 
int roundedSize; 
if (initialSize <= 8) { 
roundedSize = 1; 
while (roundedSize < initialSize) { 
roundedSize <<= 1; 
} 
} else { 
roundedSize = (initialSize + 7) / 8 * 8; 
} 
return roundedSize; 
} 

private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) { 
// The width of the original picture  
double w = options.outWidth; 
// The height of the original picture  
double h = options.outHeight; 
System.out.println("========== w="+w+",h="+h); 
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math 
.sqrt(w * h / maxNumOfPixels)); 
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min( 
Math.floor(w / minSideLength), Math.floor(h / minSideLength)); 
if (upperBound < lowerBound) { 
// return the larger one when there is no overlapping zone. 
return lowerBound; 
} 
if ((maxNumOfPixels == -1) && (minSideLength == -1)) { 
return 1; 
} else if (minSideLength == -1) { 
return lowerBound; 
} else { 
return upperBound; 
} 
} 

/** 
* @param bitmap  Pictures that need to be modified  
* @param pixels  The radian of a rounded corner  
* @return  The rounded picture  
*/ 
// The resources : 
//http://blog.csdn.net/c8822882/article/details/6906768 
public Bitmap toRoundCorner(Bitmap bitmap, int pixels) { 
Bitmap roundCornerBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
Canvas canvas = new Canvas(roundCornerBitmap); 
int color = 0xff424242;//int color = 0xff424242; 
Paint paint = new Paint(); 
paint.setColor(color); 
// To prevent the sawtooth  
paint.setAntiAlias(true); 
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
RectF rectF = new RectF(rect); 
float roundPx = pixels; 
// It's like screen cleaning  
canvas.drawARGB(0, 0, 0, 0); 
// The first 1 A rectangle with rounded corners  
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
// Let's take the original bitmap So far bitmap !!!!!!!!! Notice this understanding  
canvas.drawBitmap(bitmap, rect, rect, paint); 
return roundCornerBitmap; 
} 

} 

Related articles: