android 图片操作,Android图片操作(Bitmap)
[java] /**
* 将多个Bitmap合并成一个图片。
*
* @param int 将多个图合成多少列
* @param Bitmap... 要合成的图片
* @return
*/
public static Bitmap combineBitmaps(int columns, Bitmap... bitmaps) {
if (columns <= 0 || bitmaps == null || bitmaps.length == 0) {
throw new IllegalArgumentException("Wrong parameters: columns must > 0 and bitmaps.length must > 0.");
}
int maxWidthPerImage = 0;
int maxHeightPerImage = 0;
for (Bitmap b : bitmaps) {
maxWidthPerImage = maxWidthPerImage > b.getWidth() ? maxWidthPerImage : b.getWidth();
maxHeightPerImage = maxHeightPerImage > b.getHeight() ? maxHeightPerImage : b.getHeight();
}
int rows = 0;
if (columns >= bitmaps.length) {
rows = 1;
columns = bitmaps.length;
} else {
rows = bitmaps.length % columns == 0 ? bitmaps.length / columns : bitmaps.length / columns + 1;
}
Bitmap newBitmap = Bitmap.createBitmap(columns * maxWidthPerImage, rows * maxHeightPerImage, Config.RGB_565);
for (int x = 0; x < rows; x++) {
for (int y = 0; y < columns; y++) {
int index = x * columns + y;
if (index >= bitmaps.length)
break;
newBitmap = mixtureBitmap(newBitmap, bitmaps[index], new PointF(y * maxWidthPerImage, x * maxHeightPerImage));
}
}
return newBitmap;
}
/**
* 将多个Bitmap合并成一个图片。
*
* @param int 将多个图合成多少列
* @param Bitmap... 要合成的图片
* @return
*/
public static Bitmap combineBitmaps(int columns, Bitmap... bitmaps) {
if (columns <= 0 || bitmaps == null || bitmaps.length == 0) {
throw new IllegalArgumentException("Wrong parameters: columns must > 0 and bitmaps.length must > 0.");
}
int maxWidthPerImage = 0;
int maxHeightPerImage = 0;
for (Bitmap b : bitmaps) {
maxWidthPerImage = maxWidthPerImage > b.getWidth() ? maxWidthPerImage : b.getWidth();
maxHeightPerImage = maxHeightPerImage > b.getHeight() ? maxHeightPerImage : b.getHeight();
}
int rows = 0;
if (columns >= bitmaps.length) {
rows = 1;
columns = bitmaps.length;
} else {
rows = bitmaps.length % columns == 0 ? bitmaps.length / columns : bitmaps.length / columns + 1;
}
Bitmap newBitmap = Bitmap.createBitmap(columns * maxWidthPerImage, rows * maxHeightPerImage, Config.RGB_565);
for (int x = 0; x < rows; x++) {
for (int y = 0; y < columns; y++) {
int index = x * columns + y;
if (index >= bitmaps.length)
break;
newBitmap = mixtureBitmap(newBitmap, bitmaps[index], new PointF(y * maxWidthPerImage, x * maxHeightPerImage));
}
}
return newBitmap;
}
[java] /**
* Mix two Bitmap as one.
*
* @param bitmapOne
* @param bitmapTwo
* @param point
* where the second bitmap is painted.
* @return
*/
public static Bitmap mixtureBitmap(Bitmap first, Bitmap second, PointF fromPoint) {
if (first == null || second == null || fromPoint == null) {
return null;
}
Bitmap newBitmap = Bitmap.createBitmap(first.getWidth(), first.getHeight(), Config.ARGB_4444);
Canvas cv = new Canvas(newBitmap);
cv.drawBitmap(first, 0, 0, null);
cv.drawBitmap(second, fromPoint.x, fromPoint.y, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newBitmap;
}
/**
* Mix two Bitmap as one.
*
* @param bitmapOne
* @param bitmapTwo
* @param point
* where the second bitmap is painted.
* @return
*/
public static Bitmap mixtureBitmap(Bitmap first, Bitmap second, PointF fromPoint) {
if (first == null || second == null || fromPoint == null) {
return null;
}
Bitmap newBitmap = Bitmap.createBitmap(first.getWidth(), first.getHeight(), Config.ARGB_4444);
Canvas cv = new Canvas(newBitmap);
cv.drawBitmap(first, 0, 0, null);
cv.drawBitmap(second, fromPoint.x, fromPoint.y, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newBitmap;
}
[java] //截屏
public static Bitmap getScreenshotsForCurrentWindow(Activity activity) {
View cv = activity.getWindow().getDecorView();
Bitmap bmp = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(), Bitmap.Config.ARGB_4444);
cv.draw(new Canvas(bmp));
return bmp;
}
//截屏
public static Bitmap getScreenshotsForCurrentWindow(Activity activity) {
View cv = activity.getWindow().getDecorView();
Bitmap bmp = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(), Bitmap.Config.ARGB_4444);
cv.draw(new Canvas(bmp));
return bmp;
}
[java] //旋转图片
// Rotates the bitmap by the specified degree.
// If a new bitmap is created, the original bitmap is recycled.
public static Bitmap rotate(Bitmap b, int degrees) {
if (degrees != 0 && b != null) {
Matrix m = new Matrix();
m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
try {
b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
if (b != b2) {
b.recycle();
b = b2;
}
} catch (OutOfMemoryError ex) {
// We have no memory to rotate. Return the original bitmap.
}
}
return b;
}
//旋转图片 www.2cto.com
// Rotates the bitmap by the specified degree.
// If a new bitmap is created, the original bitmap is recycled.
public static Bitmap rotate(Bitmap b, int degrees) {
if (degrees != 0 && b != null) {
Matrix m = new Matrix();
m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
try {
b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
if (b != b2) {
b.recycle();
b = b2;
}
} catch (OutOfMemoryError ex) {
// We have no memory to rotate. Return the original bitmap.
}
}
return b;
}
[java] //可用于生成缩略图。
/**
* Creates a centered bitmap of the desired size. Recycles the input.
*
* @param source
*/
public static Bitmap extractMiniThumb(Bitmap source, int width, int height) {
return extractMiniThumb(source, width, height, true);
}
public static Bitmap extractMiniThumb(Bitmap source, int width, int height, boolean recycle) {
if (source == null) {
return null;
}
float scale;
if (source.getWidth() < source.getHeight()) {
scale = width / (float) source.getWidth();
} else {
scale = height / (float) source.getHeight();
}
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
Bitmap miniThumbnail = transform(matrix, source, width, height, false);
if (recycle && miniThumbnail != source) {
source.recycle();
}
return miniThumbnail;
}
public static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp) {
int deltaX = source.getWidth() - targetWidth;
int deltaY = source.getHeight() - targetHeight;
if (!scaleUp && (deltaX < 0 || deltaY < 0)) {
/*
* In this case the bitmap is smaller, at least in one dimension,
* than the target. Transform it by placing as much of the image as
* possible into the target and leaving the top/bottom or left/right
* (or both) black.
*/
Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b2);
int deltaXHalf = Math.max(0, deltaX / 2);
int deltaYHalf = Math.max(0, deltaY / 2);
Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf
+ Math.min(targetHeight, source.getHeight()));
int dstX = (targetWidth - src.width()) / 2;
int dstY = (targetHeight - src.height()) / 2;
Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY);
c.drawBitmap(source, src, dst, null);
return b2;
}
float bitmapWidthF = source.getWidth();
float bitmapHeightF = source.getHeight();
float bitmapAspect = bitmapWidthF / bitmapHeightF;
float viewAspect = (float) targetWidth / targetHeight;
if (bitmapAspect > viewAspect) {
float scale = targetHeight / bitmapHeightF;
if (scale < .9F || scale > 1F) {
scaler.setScale(scale, scale);
} else {
scaler = null;
}
} else {
float scale = targetWidth / bitmapWidthF;
if (scale < .9F || scale > 1F) {
scaler.setScale(scale, scale);
} else {
scaler = null;
}
}
Bitmap b1;
if (scaler != null) {
// this is used for minithumb and crop, so we want to filter here.
b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true);
} else {
b1 = source;
}
int dx1 = Math.max(0, b1.getWidth() - targetWidth);
int dy1 = Math.max(0, b1.getHeight() - targetHeight);
Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight);
if (b1 != source) {
b1.recycle();
}
return b2;
}
//可用于生成缩略图。
/**
* Creates a centered bitmap of the desired size. Recycles the input.
*
* @param source
*/
public static Bitmap extractMiniThumb(Bitmap source, int width, int height) {
return extractMiniThumb(source, width, height, true);
}
public static Bitmap extractMiniThumb(Bitmap source, int width, int height, boolean recycle) {
if (source == null) {
return null;
}
float scale;
if (source.getWidth() < source.getHeight()) {
scale = width / (float) source.getWidth();
} else {
scale = height / (float) source.getHeight();
}
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
Bitmap miniThumbnail = transform(matrix, source, width, height, false);
if (recycle && miniThumbnail != source) {
source.recycle();
}
return miniThumbnail;
}
public static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp) {
int deltaX = source.getWidth() - targetWidth;
int deltaY = source.getHeight() - targetHeight;
if (!scaleUp && (deltaX < 0 || deltaY < 0)) {
/*
* In this case the bitmap is smaller, at least in one dimension,
* than the target. Transform it by placing as much of the image as
* possible into the target and leaving the top/bottom or left/right
* (or both) black.
*/
Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b2);
int deltaXHalf = Math.max(0, deltaX / 2);
int deltaYHalf = Math.max(0, deltaY / 2);
Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf
+ Math.min(targetHeight, source.getHeight()));
int dstX = (targetWidth - src.width()) / 2;
int dstY = (targetHeight - src.height()) / 2;
Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY);
c.drawBitmap(source, src, dst, null);
return b2;
}
float bitmapWidthF = source.getWidth();
float bitmapHeightF = source.getHeight();
float bitmapAspect = bitmapWidthF / bitmapHeightF;
float viewAspect = (float) targetWidth / targetHeight;
if (bitmapAspect > viewAspect) {
float scale = targetHeight / bitmapHeightF;
if (scale < .9F || scale > 1F) {
scaler.setScale(scale, scale);
} else {
scaler = null;
}
} else {
float scale = targetWidth / bitmapWidthF;
if (scale < .9F || scale > 1F) {
scaler.setScale(scale, scale);
} else {
scaler = null;
}
}
Bitmap b1;
if (scaler != null) {
// this is used for minithumb and crop, so we want to filter here.
b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true);
} else {
b1 = source;
}
int dx1 = Math.max(0, b1.getWidth() - targetWidth);
int dy1 = Math.max(0, b1.getHeight() - targetHeight);
Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight);
if (b1 != source) {
b1.recycle();
}
return b2;
}
[java] //图片剪切
public static Bitmap cutBitmap(Bitmap mBitmap, Rect r, Bitmap.Config config) {
int width = r.width();
int height = r.height();
Bitmap croppedImage = Bitmap.createBitmap(width, height, config);
Canvas cvs = new Canvas(croppedImage);
Rect dr = new Rect(0, 0, width, height);
cvs.drawBitmap(mBitmap, r, dr, null);
return croppedImage;
}
//图片剪切
public static Bitmap cutBitmap(Bitmap mBitmap, Rect r, Bitmap.Config config) {
int width = r.width();
int height = r.height();
Bitmap croppedImage = Bitmap.createBitmap(width, height, config);
Canvas cvs = new Canvas(croppedImage);
Rect dr = new Rect(0, 0, width, height);
cvs.drawBitmap(mBitmap, r, dr, null);
return croppedImage;
}
[java] //从任一Drawable得到Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
//从任一Drawable得到Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
[java] /**
* Save Bitmap to a file.保存图片到SD卡。
*
* @param bitmap
* @param file
* @return error message if the saving is failed. null if the saving is
* successful.
* @throws IOException
*/
public static void saveBitmapToFile(Bitmap bitmap, String _file) throws IOException {
BufferedOutputStream os = null;
try {
File file = new File(_file);
// String _filePath_file.replace(File.separatorChar +
// file.getName(), "");
int end = _file.lastIndexOf(File.separator);
String _filePath = _file.substring(0, end);
File filePath = new File(_filePath);
if (!filePath.exists()) {
filePath.mkdirs();
}
file.createNewFile();
os = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
Log.e(TAG_ERROR, e.getMessage(), e);
}
}
}
}
摘自 图灵的梦
android 图片操作,Android图片操作(Bitmap)相关推荐
- android 手势旋转,android中手势操作图片的平移、缩放、旋转
在网上搜到很多都是一样的版本,只有平移和缩放的功能.我在搜到的源代码基础上添加了旋转和边界检查的功能. 代码主要分两部分,一个activity一个view,代码如下: [java] package c ...
- android 图片特征提取比对_判断ImageView当前的图片是哪个图片,然后比对资源文件的操作...
描述一下需求:我们在点击一个ImageView的时候进行,切换背景,或者是资源文件的时候,我们需要根据拿到的背景进行比对,(试了好几种方法,总是出现一些小毛病) 例如:网上的某某人写的这种: if ( ...
- android bitmap显示图片,Android_07 Android中Bitmap加载图片
一:计算机表示图形的几种方式 二:Android加载大图片 原理: [1]获取手机分辨率 [2]获取图片分辨率 创建位图工厂的配置参数 获取图片宽高 [3]计算缩放比例 [4]显示缩放后的图片 示例代 ...
- android图片适配(图片大小与屏幕密度)Bitmap占用内存计算
目录 概述 dpi 计算公式 dp与px换算公式: android系统适配图片规律: 同名图片放在不同密度的文件夹下,系统选择图片规律 同一张图片,放在不同密度的Drawable文件夹下,ImageV ...
- Android 超清大尺寸图片压缩转Base64中卡顿/速度优化问题整理(在子线程压缩Bitmap卡的主线程进度条走不动了。。。)
最近遇到需求是前后端传输图片使用的是Base64,但是前端(Android 端)图片很大(尺寸很大4480 × 2520,质量也很大7-10M),需要压缩到一定尺寸(1280 × 960,当然还可以压 ...
- Android开发——内存优化 图片处理
8. 用缓存避免内存泄漏 很常见的一个例子就是图片的三级缓存结构,分别为网络缓存,本地缓存以及内存缓存.在内存缓存逻辑类中,通常会定义这样的集合类. [java] view plaincopy p ...
- android使用ImageLoader实现图片缓存(安卓开发必备)
相信大家在学习以及实际开发中基本都会与网络数据打交道,而这其中一个非常影响用户体验的就是图片的缓存了,若是没有弄好图片缓存,用户体验会大大下降,总会出现卡顿情况,而这个问题尤其容易出现在ListVie ...
- [Android] 修改ImageView的图片颜色
有两种方法: 方法1: ImageView imageView = (ImageView) findViewById(R.id.arrow_image); Drawable tipsArrow = i ...
- android 查看多个图片,android提取视频多张图片和视频信息
android提取视频多张图片和视频信息 话说2016年的直播比较火,2017年短视频又火了.但对于开发者来说隐藏在这背后的技术才是我们所关心的,毕竟我们是靠技术吃饭的. 现在在安卓中多媒体服务比较强 ...
- android 图片方向,Android图片处理:识别图像方向并显示
在Android中使用ImageView显示图片的时候发现图片显示不正.方向偏了或者倒过来了. 解决问题非常自然想到的分两步走: 1.自己主动识别图像方向,计算旋转角度. 2.对图像进行旋转并显示. ...
最新文章
- 我们每天都在做无用功?
- C语言经典例18-求累加和
- 让Windows2008R2也能进入手柄设置(游戏控制器设置)
- 流行病学与生物统计学: 临床研究导论 Epidemiology and Biostatistics: An Introduction to Clinical Research
- Android之通过adb shell getprop、netstat命令看dns、ip
- 7-95 倒数第N个字符串 (15 分)
- 华为AppCube入选Forrester《中国低代码平台市场分析报告》
- Linux下Qt使用QAudio相关类进行音频采集,使用Windows下的Matlab软件播放
- crontab定时器
- 工具类篇——时间处理Calendar类
- 抖音时钟js css,JS+CSS3实现时钟效果(抖音)
- cocos之Gif图
- 大数据可以应用在哪些行业?
- [转载] 百家讲坛——郦波评说曾国藩家训上部 01 谁来拯救笨小孩
- 华为双系统手机可以刷成单系统_华为手机双系统,1部手机能当2部用,打开3秒就能切换,真厉害...
- (五)结合大彩屏介绍Bus Hound:cmd.Phase.ofs(rep)
- springboot基础(72):Redisson分布式锁
- 使用java进行excel的读写,两种excel的区别以及easyexcel的使用
- 人工智能安防初创公司澎思科技宣布完成千万级天使轮融资 洪泰基金领投
- 网络爬虫逆向(全国建筑市场监管公共服务平台)