本文实例总结了Android编程之图片相关代码。分享给大家供大家参考,具体如下: 1. Bitmap转化为字符串: public static String bitmap
本文实例总结了Android编程之图片相关代码。分享给大家供大家参考,具体如下:
1. Bitmap转化为字符串:
public static String bitmapToString(Bitmap bitmap) {
// 将Bitmap转换成字符串
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(CompressFORMat.PNG, 100, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
2.字符串转化为Bitmap:
public static Bitmap stringToBitmap(String string) {
// 将字符串转换成Bitmap类型
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
3.Bitmap转化为Drawable:
public static Drawable bitmapToDrawable(Bitmap bitmap) {
if (bitmap == null)
return null;
if (160 != bitmap.getDensity()) {
bitmap.setDensity(160);
}
return new BitmapDrawable(bitmap);
}
根据图片资源ID获取Drawable对象:
public static Drawable resourceToDrawable(Context context,int id) {
return null == context ? null : bitmapToDrawable(BitmapFactory.decodeResource(context.getResources(), id));
}
byte数组转换Drawble对象:
public static Drawable byteArrayToDrawable(byte[] bytes) {
return null == bytes ? null : bitmapToDrawable(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
}
4.Drawable转化为bitmap:
public static Bitmap drawableToBitmap(Drawable drawable) {
return null == drawable ? null : ((BitmapDrawable) drawable).getBitmap();
}
5.byte数组转换Bitmap对象:
public static Bitmap byteArrayToBitmap(byte[] bytes) {
return null == bytes ? null : BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
6.图片去色,返回灰度图片(老式图片):
public static Bitmap toGrayscale(Bitmap bitmap) {
int width,height;
height = bitmap.getHeight();
width = bitmap.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmap, 0, 0, paint);
return bmpGrayscale;
}
7.对图片进行缩放:
public static Bitmap getScaleImage(String url,int requireSize) {
BitmapFactory.Options o = new BitmapFactory.Options();
// 此属性表示图片不加载到内存,只是读取图片的属性,包括图片的高宽
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(url, o);
int width_tmp = o.outWidth,height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < requireSize || height_tmp / 2 < requireSize)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bmp = BitmapFactory.decodeFile(url, o2);
return bmp;
}
8.获得图片的倒影,同时倒影渐变效果:
public static Bitmap createMirro(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int shadow_height = 15;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
// shadow effect
int alpha = 0x00000000;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int index = y * width + x;
int r = (pixels[index] >> 16) & 0xff;
int g = (pixels[index] >> 8) & 0xff;
int b = pixels[index] & 0xff;
pixels[index] = alpha | (r << 16) | (g << 8) | b;
}
if (y >= (height - shadow_height)) {
alpha = alpha + 0x1F000000;
}
}
// invert effect
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int y = 0; y < height; y++) {
bm.setPixels(pixels, y * width, width, 0, height - y - 1, width, 1);
}
return Bitmap.createBitmap(bm, 0, 0, width, shadow_height);
}
9.保存图片到SDCard:
public static void saveImgToLocal(String imagePath, Bitmap bm) {
if (bm == null || imagePath == null || "".equals(imagePath)) {
return;
}
File f = new File(imagePath);
if (f.exists()) {
return;
} else {
try {
File parentFile = f.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
f.createNewFile();
FileOutputStream fos;
fos = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
f.delete();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
f.delete();
}
}
}
10.从SDCard中获取图片:
public static Bitmap getImageFromLocal(String imagePath) {
File file = new File(imagePath);
if (file.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
file.setLastModified(System.currentTimeMillis());
return bitmap;
}
return null;
}
11.图片压缩处理:
public static Bitmap compressBitmapFromResourse(Context context, int resId, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(context.getResources(), resId, options);
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
options.inSampleSize = inSampleSize;
//使用计算得到的inSampleSize值再次解析图片
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(context.getResources(), resId, options);
}
12. 获取可用内存的最大值(App使用内存超出这个值会引起OutOfMemory异常):
private int getMaxMemoryForApp() {
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
return maxMemory;
}
13.将图片裁剪成圆圈:
public static Bitmap circlePic(Bitmap bitmap){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int r = width < height ? width/2:height/2;//圆的半径,取宽和高中较小的,以便于显示没有空白
Bitmap outBitmap = Bitmap.createBitmap(r*2, r*2, Bitmap.Config.ARGB_8888);//创建一个刚好2r大小的Bitmap
Canvas canvas = new Canvas(outBitmap);
final int color =0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect( width < height ? 0 : (width-height)/2, width < height ? (height-width)/2 - 10 : -10,
width < height ? width : (width+height)/2, (width < height ? (height+width)/2 - 10: height - 10));
//创建一个直径大小的正方形,用于设置canvas的显示与设置画布截取
final Rect rect2 = new Rect( 0, 0, r*2, r*2);
//提高精度,用于消除锯齿
final RectF rectF = new RectF(rect2);
//下面是设置画笔和canvas
paint.setAntiAlias(true);
canvas.drawARGB(0,0,0,0);
paint.setColor(color);
//设置圆角,半径都为r,大小为rect2
canvas.drawRoundRect(rectF, r, r, paint);
//设置图像重叠时的显示方式
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//绘制图像到canvas
canvas.drawBitmap(bitmap, rect, rect2, paint);
return outBitmap;
}
}
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:Android编程实现给Button添加图片和文字的方法Android代码实现图片和文字上下布局android编程实现系统图片剪裁的方法android编程实现图片库的封装方法Android编程中图片特效处理方法小结Android图片转换器代码分享Android启动相机拍照并返回图片Android编程之图片颜色处理方法Android编程学习之异步加载图片的方法android图片类型之间相互转换实现代码Android实现ListView异步加载图片的方法Android开发从相机或相册获取图片裁剪
--结束END--
本文标题: Android编程之图片相关代码集锦
本文链接: https://www.lsjlt.com/news/26295.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0