Android 圖片操作,Image實戰
平時有很多時候會涉及到圖片的操作,如獲取縮圖,裁剪圖片,旋轉圖片,獲取尺寸,改變大學,drawable與bitmap相互轉換等問題。本文給打擊總結一些常用的操作,比較基礎但是實用。
一,drawable與bitmap相互轉換
drawable資源轉換為bitmap物件
/**
* 將已有的drawable資源轉換為bitmap物件
*/
public static Bitmap drawableToBitamp(Drawable drawable)
{
BitmapDrawable bd = (BitmapDrawable) drawable;
Bitmap bitmap = bd.getBitmap();
return bitmap;
}
或者
/**
* 將已有的drawable資源轉換為bitmap物件,方式二
*/
public static Bitmap drawable2Bitamp(Drawable drawable)
{
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
//System.out.println("Drawable轉Bitmap");
Bitmap.Config config =
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(w,h,config);
//注意,下面三行程式碼要用到,否在在View或者surfaceview裡的canvas.drawBitmap會看不到圖
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
Bitmap轉化為drawable
/**
* Bitmap轉化為drawable
*/
public static Drawable bitmap2Drawable(Bitmap bitmap) {
return new BitmapDrawable(bitmap);
}
二,縮放圖片到指定的大小
//圖片縮放到指定的寬高
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newbmp;
}
三,儲存bitmap到本地
//將bitmap儲存到sdcard中
public static File saveBitmapToSDCard(Bitmap photoBitmap,String path,String photoName){
if (FileUtils.isSDCardAvailable()) {
File dir = new File(path);
if (!dir.exists()){
dir.mkdirs();
}
File photoFile = new File(path , photoName + ".jpg");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {
if (photoBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)) {
fileOutputStream.flush();
//fileOutputStream.close();
return photoFile;
}
}
} catch (FileNotFoundException e) {
photoFile.delete();
e.printStackTrace();
} catch (Exception e) {
photoFile.delete();
e.printStackTrace();
} finally{
try {
if (fileOutputStream!=null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
四,把bitmap轉換成String
/**
* 把bitmap轉換成String
*/
public static String bitmapToString(String filePath) {
Bitmap bm = getSmallBitmap(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}
五,計算圖片的縮放值
/**
* 計算圖片的縮放值
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height andwidth
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
六,根據路徑獲得圖片並返回壓縮的bitmap
/**
* 根據路徑獲得圖片並壓縮,返回bitmap用於顯示
*/
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
七,判斷照片角度
/**
* 判斷照片角度
* @param path
* @return
*/
public static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
八,旋轉照片
/**
* 旋轉照片
* @param bitmap
* @param degress
* @return
*/
public static Bitmap rotateBitmap(Bitmap bitmap,int degress) {
if (bitmap != null) {
Matrix m = new Matrix();
m.postRotate(degress);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), m, true);
return bitmap;
}
return bitmap;
}
九,處理手機相片的角度旋轉
/**
* 壓縮圖片,處理某些手機拍照角度旋轉的問題
*/
public static String compressImage(String filePath, String targetPath, int quality) {
Bitmap bm = getSmallBitmap(filePath);
int degree = readPictureDegree(filePath);
if(degree!=0){//旋轉照片角度
bm=rotateBitmap(bm,degree);
}
File outputFile=new File(targetPath);
try {
if (!outputFile.exists()) {
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
}else{
outputFile.delete();
}
FileOutputStream out = new FileOutputStream(outputFile);
bm.compress(Bitmap.CompressFormat.JPEG, quality, out);
}catch (Exception e){
LogUtils.debug(TAG,"compressImage error"+e.toString());
}
return outputFile.getPath();
}
未完待續,歡迎交流,杜乾,Dusan,Q 291902259。
相關文章
- 關於圖片適配不同尺寸的image View(實戰)View
- android圖片壓縮不失真實戰Android
- Android實現圖片非同步載入操作Android非同步
- Android Xfermode 實戰 實現圓形、圓角圖片Android
- android-smart-image-view圖片載入簡單使用AndroidView
- Golang 圖片處理 — image 庫Golang
- 前端的圖片壓縮image-compressor(可在圖片上傳前實現圖片壓縮)前端
- java Html2Image 實現html轉圖片功能JavaHTML
- intervention/image 伺服器圖片裁剪伺服器
- CSS-背景圖片|background-imageCSS
- Laravel 使用 Intervention/image 處理圖片Laravel
- Image Optimizer for Mac(圖片壓縮工具)Mac
- vue el-image 顯示圖片Vue
- 圖片爬取實戰一
- Nginx Image Module圖片縮圖 水印處理模組Nginx
- Flutter 入門與實戰(七):使用 cached_image_network 優化圖片載入體驗Flutter優化
- 002.00 圖片處理 PIL.Image
- 圖片大小調整工具:Image Resizer for MacMac
- 二維CANVAS基本語法(圖片Image)Canvas
- opencv圖片操作OpenCV
- 鴻蒙HarmonyOS實戰-ArkUI元件(Image)鴻蒙UI元件
- 實戰:圖片上傳元件開發元件
- 圖片懶載入外掛實戰
- CSS-實戰-互動式圖片CSS
- Android使用LruCache、DiskLruCache實現圖片快取+圖片瀑布流Android快取
- Android 圖片平鋪實現方式Android
- Oracle 操作圖片 (轉)Oracle
- 鴻蒙OS前端開發入門指南:網路圖片_Image渲染網路圖片鴻蒙前端
- Android 載入網路圖片 以及實現圓角圖片效果Android
- Flutter 基礎控制元件篇-->圖片(Image)、圖示(Icon)Flutter控制元件
- 圖片操作系列 —(2)手勢旋轉圖片
- Python3 大型網路爬蟲實戰 003 — scrapy 大型靜態圖片網站爬蟲專案實戰 — 實戰:爬取 169美女圖片網 高清圖片Python爬蟲網站
- 文件轉換成圖片軟體(convert document to Image)
- Nginx安裝http_image_filter_module圖片裁剪模組NginxHTTPFilter
- django 實現圖片上傳和顯示操作Django
- Android圖片突出Android
- 圖片操作系列 —(1)手勢縮放圖片功能
- uniapp實戰——完成圖片的預覽功能APP