package org.cocos2dx.multiphoto;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.security.MessageDigest;
import org.cocos2dx.lib.Cocos2dxActivity;
import com.quanyu.mahjong.appstore.R;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
/**
* Tools for handler picture
*
* @author Katty.Chen
*
*/
public final class ImageTools {
static Context context = Cocos2dxActivity.getContext();
/**
* Save image to the SD card
* @param photoBitmap
* @param photoName
* @param path
*/
public static void savePhotoToSDCard(Bitmap photoBitmap,String path,String photoName){
if (checkSDCardAvailable()) {
File photoFile = new File(path , photoName);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
fileOutputStream.flush();
}
}
} catch (Exception e) {
photoFile.delete();
e.printStackTrace();
}finally{
try {
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void savePhotoToSDCard(Bitmap photoBitmap,String path){
if (checkSDCardAvailable()) {
File photoFile = new File(path);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
fileOutputStream.flush();
}
}
} catch (Exception e) {
photoFile.delete();
e.printStackTrace();
} finally{
try {
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* Check the SD card
* @return
*/
public static boolean checkSDCardAvailable(){
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
/**
* 根据路径加载bitmap
*
* @param path
* 路径
* @param w
* 款
* @param h
* 长
* @return
*/
public static final Bitmap convertToBitmap(String path, int w, int h) {
try {
BitmapFactory.Options opts = new BitmapFactory.Options();
// 设置为ture只获取图片大小
opts.inJustDecodeBounds = true;
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
// 返回为空
BitmapFactory.decodeFile(path, opts);
int width = opts.outWidth;
int height = opts.outHeight;
float scaleWidth = 0.f, scaleHeight = 0.f;
if (width > w || height > h) {
// 缩放
scaleWidth = ((float) width) / w;
scaleHeight = ((float) height) / h;
}
opts.inJustDecodeBounds = false;
float scale = Math.max(scaleWidth, scaleHeight);
opts.inSampleSize = (int) scale;
WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
Bitmap bMapRotate = Bitmap.createBitmap(weak.get(), 0, 0, weak.get().getWidth(), weak.get().getHeight(), null, true);
if (bMapRotate != null) {
return bMapRotate;
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getMD5(String s) {
// TODO Auto-generated method stub
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(s.getBytes("utf-8"));
final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
StringBuilder ret = new StringBuilder(bytes.length * 2);
for (int i=0; i<bytes.length; i++) {
ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
}
return ret.toString();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
public static final String TYPE_JPG = "jpg";
public static final String TYPE_GIF = "gif";
public static final String TYPE_PNG = "png";
public static final String TYPE_BMP = "bmp";
public static final String TYPE_UNKNOWN = "unknown";
/**
* byte数组转换成16进制字符串
* @param src
* @return
*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/**
* 根据文件流判断图片类型
* @param fis
* @return jpg/png/gif/bmp
*/
public static String getPicType(FileInputStream fis) {
//读取文件的前几个字节来判断图片格式
byte[] b = new byte[4];
try {
fis.read(b, 0, b.length);
String type = bytesToHexString(b).toUpperCase();
if (type.contains("FFD8FF")) {
return TYPE_JPG;
} else if (type.contains("89504E47")) {
return TYPE_PNG;
} else if (type.contains("47494638")) {
return TYPE_GIF;
} else if (type.contains("424D")) {
return TYPE_BMP;
}else{
return TYPE_UNKNOWN;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static void saveBitmapToSd(Bitmap bitmap, String filePath){
FileOutputStream outputStream = null;
try {
File file = new File(filePath);
if(file.exists() || file.isDirectory()){
file.delete();
}
file.createNewFile();
outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(outputStream != null){
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static Bitmap decodeUriAsBitmap(Uri uri)
{
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
return bitmap;
}
/**
* 裁剪图片转换圆角缩略图
* @param source 源 Bitmap
* @return 圆角缩略图roundThmubBitmap
*/
public static Bitmap roundThmubBitmap(Bitmap source) {
Bitmap bitmap = zoomBitmap(source, 217, 270);
Bitmap roundThmubBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(roundThmubBitmap);
Paint paint = new Paint();
paint.setColor(0xFFFF0000); // 任何不透明的颜色均可。(作为掩码色)
paint.setAntiAlias(true); // 开启抗锯齿,防止圆角毛躁.
// 填充一个圆角矩形
final float radius = 10.0f;
RectF rc = new RectF(0,0,bitmap.getWidth(),bitmap.getHeight());
canvas.drawRoundRect(rc, radius, radius, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
// source.recycle();
return roundThmubBitmap;
}
/**
* 按宽/高缩放图片到指定大小并进行裁剪得到中间部分图片
* @param vw 缩放后指定的宽度
* @param vh 缩放后指定的高度
* @return 缩放后的中间部分图片 Bitmap
*/
public static Bitmap zoomBitmap(Bitmap bitmap, float vw, float vh) {
float width = bitmap.getWidth();//获得图片宽高
float height = bitmap.getHeight();
float scaleWidht, scaleHeight, x, y;//图片缩放倍数以及x,y轴平移位置
Bitmap newbmp = null; //新的图片
Matrix matrix = new Matrix();//变换矩阵
if ((width/height)<=vw/vh){//当宽高比大于所需要尺寸的宽高比时以宽的倍数为缩放倍数
scaleWidht = vw / width;
scaleHeight = scaleWidht;
y = ((height*scaleHeight - vh) / 2)/scaleHeight;// 获取bitmap源文件中y坐标需要偏移的像数大小
x = 0;
}else {
scaleWidht = vh / height;
scaleHeight = scaleWidht;
x = ((width*scaleWidht - vw ) / 2)/scaleWidht;// 获取bitmap源文件中x坐标需要偏移的像数大小
y = 0;
}
matrix.postScale(scaleWidht / 1f, scaleHeight / 1f);
try {
if (width - x > 0 && height - y > 0&&bitmap!=null)//获得新的图片 (原图,x轴起始位置,y轴起始位置,X轴结束位置,Y轴结束位置,缩放矩阵,是否过滤原图)为防止报错取绝对值
newbmp = Bitmap.createBitmap(bitmap, (int) Math.abs(x), (int) Math.abs(y), (int) Math.abs(width - x*2),
(int) Math.abs(height - y*2), matrix, false);// createBitmap()方法中定义的参数x+width要小于或等于bitmap.getWidth(),y+height要小于或等于bitmap.getHeight()
} catch (Exception e) {//如果报错则返回原图,不至于为空白
e.printStackTrace();
return bitmap;
}
return newbmp;
}
/**
* 叠加二维码到海报上
*/
public static Bitmap makePoster() {
// 防止出现Immutable bitmap passed to Canvas constructor错误
AssetManager assetManager = context.getAssets();
InputStream is1 = null;
InputStream is2 = null;
try {
is1 = assetManager.open("res/poster.png");
is2 = assetManager.open("res/QRCode.jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap backMap = BitmapFactory.decodeStream(is1).copy(Bitmap.Config.ARGB_8888, true);
Bitmap codeBmp = BitmapFactory.decodeStream(is2).copy(Bitmap.Config.ARGB_8888, true);
Bitmap codeMap = zoomBitmap(codeBmp, 220, 220);
Bitmap newBitmap = null;
newBitmap = Bitmap.createBitmap(backMap);
Canvas canvas = new Canvas(newBitmap);
Paint paint = new Paint();
int w = backMap.getWidth();
int h = backMap.getHeight();
int w_qrc = codeMap.getWidth();
int h_qrc = codeMap.getHeight();
paint.setColor(Color.GRAY);
paint.setAlpha(0);
canvas.drawRect(0, 0, backMap.getWidth()/2, backMap.getHeight()/2, paint);
// canvas.drawRect(0, 0, 0, 0, paint);
paint = new Paint();
canvas.drawBitmap(codeMap, Math.abs(w/2 - w_qrc/2), 1100, paint);
canvas.save(Canvas.ALL_SAVE_FLAG); // 存储新合成的图片
// canvas.translate(0,600);
canvas.restore();
return newBitmap;
}
}
android上图像处理的一些方法,类型判断存储缩放圆角叠合等
猜你喜欢
转载自blog.csdn.net/Willow_Chen/article/details/86590082
今日推荐
周排行