1、图片重画
/**
* 将图片以指定尺寸重画
*
* @param map
* @param height
* @param width
* @return
*/
public Bitmap chagedImageSize(Bitmap map, int height, int width)
{
int imageWidth = map.getWidth();
int imageHeight = map.getHeight();
float proWidth = width / (float) imageWidth;
float proHeight = height / (float) imageHeight;
// 等比缩放
if (proWidth > proHeight) {
proWidth = proHeight;
} else {
proHeight = proWidth;
}
Matrix matrix = new Matrix();
matrix.postScale(proWidth, proHeight);
return Bitmap.createBitmap(map, 0, 0, imageWidth, imageHeight, matrix, true);
}
2、从SD卡中获取图片并转换成Bitmap
public static Bitmap getBitmapFromSd(String path)
{
Bitmap bitmap=null;
InputStream is=null;
try
{
File file=new File(path);
if(file.exists()&&file.isFile())
{
is = StreamUtil.fileToInputStream(new File(path));
byte[] result=StreamUtil.readInputStream(is);
bitmap= BitmapFactory.decodeByteArray(result, 0, result.length);
is.close();
}
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(null!=is)
{
is=null;
}
}
return bitmap;
}
3、Bitmap压缩
public static Bitmap compressImage(Bitmap image)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 15)
{ // 循环判断如果压缩后图片是否大于10kb,大于继续压缩
baos.reset();// 重置baos即清空baos
options -= 10;// 每次都减少10
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
return bitmap;
}
4、图片转成string,或用于上传图片,接收端再转换
public static String convertIconToString(Bitmap bitmap)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
bitmap.compress(CompressFormat.PNG, 100, baos);
byte[] appicon = baos.toByteArray();// 转为byte数组
return Base64.encodeToString(appicon, Base64.DEFAULT);
}
5、网络下载图片转换
public static byte[] getImageByte(String path)
{
byte[] res=null;
URL url=null;
InputStream inputStream=null;
HttpURLConnection conn =null;
try
{
url = new URL(path);
conn= (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
inputStream=conn.getInputStream();
res = StreamUtil.readInputStream(inputStream);
inputStream.close();
conn.disconnect();
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
conn=null;
inputStream=null;
url=null;
}
return res;
}
public static void writeToSd(byte res[],String filePath)
{
InputStream is=null;
try
{
is=new ByteArrayInputStream(res);
StreamUtil.inputStreamToFile(is, new File(filePath));
is.close();
}catch(Exception e)
{
e.printStackTrace();
}finally
{
if(null!=is)
{
is=null;
}
}
}
public static byte[] bitmapBytes(Bitmap bm)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
6、图片圆角处理
//图片圆角处理
public static Bitmap toRoundCorner(Bitmap bitmap, int dpRound,Context context)
{
int pxRound=dip2px(context, dpRound);
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
//final int color = 0xff424242;
//final int color=R.color.pt_goods_center_textcolor;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pxRound;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
//paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}