位置: 编程技术 - 正文

工具类(4)图片操作工具类(工具的图)

编辑:rootadmin

推荐整理分享工具类(4)图片操作工具类(工具的图),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:工具图示,工具类图片,工具图案设计,工具的图,工具图片及名称,工具图案设计,工具图示,工具类图片,内容如对您有帮助,希望把文章链接给更多的朋友!

本工具类又开源项目中获得

工具类(4)图片操作工具类(工具的图)

public class ImageUtils {public final static String SDCARD_MNT = "/mnt/sdcard";public final static String SDCARD = "/sdcard";/** 请求相册 */public static final int REQUEST_CODE_GETIMAGE_BYSDCARD = 0;/** 请求相机 */public static final int REQUEST_CODE_GETIMAGE_BYCAMERA = 1;/** 请求裁剪 */public static final int REQUEST_CODE_GETIMAGE_BYCROP = 2;/*** 写图片文件 在Android系统中,文件保存在 /data/data/PACKAGE_NAME/files 目录下* * @throws IOException*/public static void saveImage(Context context, String fileName, Bitmap bitmap)throws IOException {saveImage(context, fileName, bitmap, );}public static void saveImage(Context context, String fileName,Bitmap bitmap, int quality) throws IOException {if (bitmap == null || fileName == null || context == null)return;FileOutputStream fos = context.openFileOutput(fileName,Context.MODE_PRIVATE);ByteArrayOutputStream stream = new ByteArrayOutputStream();bitmap.compress(CompressFormat.JPEG, quality, stream);byte[] bytes = stream.toByteArray();fos.write(bytes);fos.close();}/*** 写图片文件到SD卡* * @throws IOException*/public static void saveImageToSD(Context ctx, String filePath,Bitmap bitmap, int quality) throws IOException {if (bitmap != null) {File file = new File(filePath.substring(0,filePath.lastIndexOf(File.separator)));if (!file.exists()) {file.mkdirs();}BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));bitmap.compress(CompressFormat.JPEG, quality, bos);bos.flush();bos.close();if(ctx!=null){scanPhoto(ctx, filePath);}}}public static void saveBackgroundImage(Context ctx, String filePath, Bitmap bitmap, int quality) throws IOException{if (bitmap != null) {File file = new File(filePath.substring(0,filePath.lastIndexOf(File.separator)));if (!file.exists()) {file.mkdirs();}BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));bitmap.compress(CompressFormat.PNG, quality, bos);bos.flush();bos.close();if(ctx!=null){scanPhoto(ctx, filePath);}}}/*** 让Gallery上能马上看到该图片*/private static void scanPhoto(Context ctx, String imgFileName) {Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);File file = new File(imgFileName);Uri contentUri = Uri.fromFile(file);mediaScanIntent.setData(contentUri);ctx.sendBroadcast(mediaScanIntent);}/*** 获取bitmap* * @param context* @param fileName* @return*/public static Bitmap getBitmap(Context context, String fileName) {FileInputStream fis = null;Bitmap bitmap = null;try {fis = context.openFileInput(fileName);bitmap = BitmapFactory.decodeStream(fis);} catch (FileNotFoundException e) {e.printStackTrace();} catch (OutOfMemoryError e) {e.printStackTrace();} finally {try {fis.close();} catch (Exception e) {}}return bitmap;}/*** 获取bitmap* * @param filePath* @return*/public static Bitmap getBitmapByPath(String filePath) {return getBitmapByPath(filePath, null);}public static Bitmap getBitmapByPath(String filePath,BitmapFactory.Options opts) {FileInputStream fis = null;Bitmap bitmap = null;try {File file = new File(filePath);fis = new FileInputStream(file);bitmap = BitmapFactory.decodeStream(fis, null, opts);} catch (FileNotFoundException e) {e.printStackTrace();} catch (OutOfMemoryError e) {e.printStackTrace();} finally {try {fis.close();} catch (Exception e) {}}return bitmap;}/*** 获取bitmap* * @param file* @return*/public static Bitmap getBitmapByFile(File file) {FileInputStream fis = null;Bitmap bitmap = null;try {fis = new FileInputStream(file);bitmap = BitmapFactory.decodeStream(fis);} catch (FileNotFoundException e) {e.printStackTrace();} catch (OutOfMemoryError e) {e.printStackTrace();} finally {try {fis.close();} catch (Exception e) {}}return bitmap;}/*** 使用当前时间戳拼接一个唯一的文件名* * @param format* @return*/public static String getTempFileName() {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SS");String fileName = format.format(new Timestamp(System.currentTimeMillis()));return fileName;}/*** 获取照相机使用的目录* * @return*/public static String getCamerPath() {return Environment.getExternalStorageDirectory() &#; File.separator&#; "FounderNews" &#; File.separator;}/*** 判断当前Url是否标准的 * @param uri* @return*/public static String getAbsolutePathFromNoStandardUri(Uri mUri) {String filePath = null;String mUriString = mUri.toString();mUriString = Uri.decode(mUriString);String pre1 = " &#; SDCARD &#; File.separator;String pre2 = " &#; SDCARD_MNT &#; File.separator;if (mUriString.startsWith(pre1)) {filePath = Environment.getExternalStorageDirectory().getPath()&#; File.separator &#; mUriString.substring(pre1.length());} else if (mUriString.startsWith(pre2)) {filePath = Environment.getExternalStorageDirectory().getPath()&#; File.separator &#; mUriString.substring(pre2.length());}return filePath;}/*** 通过uri获取文件的绝对路径* * @param uri* @return*/@SuppressWarnings("deprecation")public static String getAbsoluteImagePath(Activity context, Uri uri) {String imagePath = "";String[] proj = { MediaStore.Images.Media.DATA };Cursor cursor = context.managedQuery(uri, proj, // Which columns to// returnnull, // WHERE clause; which rows to return (all rows)null, // WHERE clause selection arguments (none)null); // Order-by clause (ascending by name)if (cursor != null) {int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);if (cursor.getCount() > 0 && cursor.moveToFirst()) {imagePath = cursor.getString(column_index);}}return imagePath;}/*** 获取图片缩略图 只有Android2.1以上版本支持* * @param imgName* @param kind* MediaStore.Images.Thumbnails.MICRO_KIND* @return*/@SuppressWarnings("deprecation")public static Bitmap loadImgThumbnail(Activity context, String imgName,int kind) {Bitmap bitmap = null;String[] proj = { MediaStore.Images.Media._ID,MediaStore.Images.Media.DISPLAY_NAME };Cursor cursor = context.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,MediaStore.Images.Media.DISPLAY_NAME &#; "='" &#; imgName &#; "'",null, null);if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {ContentResolver crThumb = context.getContentResolver();BitmapFactory.Options options = new BitmapFactory.Options();options.inSampleSize = 1;bitmap = MethodsCompat.getThumbnail(crThumb, cursor.getInt(0),kind, options);}return bitmap;}public static Bitmap loadImgThumbnail(String filePath, int w, int h) {Bitmap bitmap = getBitmapByPath(filePath);return zoomBitmap(bitmap, w, h);}/*** 获取SD卡中最新图片路径* * @return*/public static String getLatestImage(Activity context) {String latestImage = null;String[] items = { MediaStore.Images.Media._ID,MediaStore.Images.Media.DATA };Cursor cursor = context.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, items, null,null, MediaStore.Images.Media._ID &#; " desc");if (cursor != null && cursor.getCount() > 0) {cursor.moveToFirst();for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {latestImage = cursor.getString(1);break;}}return latestImage;}/*** 计算缩放图片的宽高* * @param img_size* @param square_size* @return*/public static int[] scaleImageSize(int[] img_size, int square_size) {if (img_size[0] <= square_size && img_size[1] <= square_size)return img_size;double ratio = square_size/ (double) Math.max(img_size[0], img_size[1]);return new int[] { (int) (img_size[0] * ratio),(int) (img_size[1] * ratio) };}/*** 创建缩略图* * @param context* @param largeImagePath* 原始大图路径* @param thumbfilePath* 输出缩略图路径* @param square_size* 输出图片宽度* @param quality* 输出图片质量* @throws IOException*/public static void createImageThumbnail(Context context,String largeImagePath, String thumbfilePath, int square_size,int quality) throws IOException {BitmapFactory.Options opts = new BitmapFactory.Options();opts.inSampleSize = 1;// 原始图片bitmapBitmap cur_bitmap = getBitmapByPath(largeImagePath, opts);if (cur_bitmap == null)return;// 原始图片的高宽int[] cur_img_size = new int[] { cur_bitmap.getWidth(),cur_bitmap.getHeight() };// 计算原始图片缩放后的宽高int[] new_img_size = scaleImageSize(cur_img_size, square_size);// 生成缩放后的bitmapBitmap thb_bitmap = zoomBitmap(cur_bitmap, new_img_size[0],new_img_size[1]);// 生成缩放后的图片文件saveImageToSD(null,thumbfilePath, thb_bitmap, quality);}/*** 放大缩小图片* * @param bitmap* @param w* @param h* @return*/public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {Bitmap newbmp = null;if (bitmap != null) {int width = bitmap.getWidth();int height = bitmap.getHeight();Matrix matrix = new Matrix();float scaleWidht = ((float) w / width);float scaleHeight = ((float) h / height);matrix.postScale(scaleWidht, scaleHeight);newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,true);}return newbmp;}public static Bitmap scaleBitmap(Bitmap bitmap) {// 获取这个图片的宽和高int width = bitmap.getWidth();int height = bitmap.getHeight();// 定义预转换成的图片的宽度和高度int newWidth = ;int newHeight = ;// 计算缩放率,新尺寸除原始尺寸float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;// 创建操作图片用的matrix对象Matrix matrix = new Matrix();// 缩放图片动作matrix.postScale(scaleWidth, scaleHeight);// 旋转图片 动作// matrix.postRotate();// 创建新的图片Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,matrix, true);return resizedBitmap;}/*** (缩放)重绘图片* * @param context* Activity* @param bitmap* @return*/public static Bitmap reDrawBitMap(Activity context, Bitmap bitmap) {DisplayMetrics dm = new DisplayMetrics();context.getWindowManager().getDefaultDisplay().getMetrics(dm);int rHeight = dm.heightPixels;int rWidth = dm.widthPixels;// float rHeight=dm.heightPixels/dm.density&#;0.5f;// float rWidth=dm.widthPixels/dm.density&#;0.5f;// int height=bitmap.getScaledHeight(dm);// int width = bitmap.getScaledWidth(dm);int height = bitmap.getHeight();int width = bitmap.getWidth();float zoomScale;/** 方式1 **/// if(rWidth/rHeight>width/height){//以高为准// zoomScale=((float) rHeight) / height;// }else{// //if(rWidth/rHeight<width/height)//以宽为准// zoomScale=((float) rWidth) / width;// }/** 方式2 **/// if(width*1.5 >= height) {//以宽为准// if(width >= rWidth)// zoomScale = ((float) rWidth) / width;// else// zoomScale = 1.0f;// }else {//以高为准// if(height >= rHeight)// zoomScale = ((float) rHeight) / height;// else// zoomScale = 1.0f;// }/** 方式3 **/if (width >= rWidth)zoomScale = ((float) rWidth) / width;elsezoomScale = 1.0f;// 创建操作图片用的matrix对象Matrix matrix = new Matrix();// 缩放图片动作matrix.postScale(zoomScale, zoomScale);Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true);return resizedBitmap;}/*** 将Drawable转化为Bitmap* * @param drawable* @return*/public static Bitmap drawableToBitmap(Drawable drawable) {int width = drawable.getIntrinsicWidth();int height = drawable.getIntrinsicHeight();Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_: Bitmap.Config.RGB_);Canvas canvas = new Canvas(bitmap);drawable.setBounds(0, 0, width, height);drawable.draw(canvas);return bitmap;}/*** 获得圆角图片的方法* * @param bitmap* @param roundPx* 一般设成* @return*/public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Config.ARGB_);Canvas canvas = new Canvas(output);final int color = 0xff;final Paint paint = new Paint();final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());final RectF rectF = new RectF(rect);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;}/*** 获得带倒影的图片方法* * @param bitmap* @return*/public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {final int reflectionGap = 4;int width = bitmap.getWidth();int height = bitmap.getHeight();Matrix matrix = new Matrix();matrix.preScale(1, -1);Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,width, height / 2, matrix, false);Bitmap bitmapWithReflection = Bitmap.createBitmap(width,(height &#; height / 2), Config.ARGB_);Canvas canvas = new Canvas(bitmapWithReflection);canvas.drawBitmap(bitmap, 0, 0, null);Paint deafalutPaint = new Paint();canvas.drawRect(0, height, width, height &#; reflectionGap, deafalutPaint);canvas.drawBitmap(reflectionImage, 0, height &#; reflectionGap, null);Paint paint = new Paint();LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,bitmapWithReflection.getHeight() &#; reflectionGap, 0xffffff,0xffffff, TileMode.CLAMP);paint.setShader(shader);// Set the Transfer mode to be porter duff and destination inpaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));// Draw a rectangle using the paint with our linear gradientcanvas.drawRect(0, height, width, bitmapWithReflection.getHeight()&#; reflectionGap, paint);return bitmapWithReflection;}/*** 将bitmap转化为drawable* * @param bitmap* @return*/public static Drawable bitmapToDrawable(Bitmap bitmap) {Drawable drawable = new BitmapDrawable(bitmap);return drawable;}/*** 获取图片类型* * @param file* @return*/public static String getImageType(File file) {if (file == null || !file.exists()) {return null;}InputStream in = null;try {in = new FileInputStream(file);String type = getImageType(in);return type;} catch (IOException e) {return null;} finally {try {if (in != null) {in.close();}} catch (IOException e) {}}}/*** 获取图片的类型信息* * @param in* @return* @see #getImageType(byte[])*/public static String getImageType(InputStream in) {if (in == null) {return null;}try {byte[] bytes = new byte[8];in.read(bytes);return getImageType(bytes);} catch (IOException e) {return null;}}/*** 获取图片的类型信息* * @param bytes* 2~8 byte at beginning of the image file* @return image mimetype or null if the file is not image*/public static String getImageType(byte[] bytes) {if (isJPEG(bytes)) {return "image/jpeg";}if (isGIF(bytes)) {return "image/gif";}if (isPNG(bytes)) {return "image/png";}if (isBMP(bytes)) {return "application/x-bmp";}return null;}private static boolean isJPEG(byte[] b) {if (b.length < 2) {return false;}return (b[0] == (byte) 0xFF) && (b[1] == (byte) 0xD8);}private static boolean isGIF(byte[] b) {if (b.length < 6) {return false;}return b[0] == 'G' && b[1] == 'I' && b[2] == 'F' && b[3] == '8'&& (b[4] == '7' || b[4] == '9') && b[5] == 'a';}private static boolean isPNG(byte[] b) {if (b.length < 8) {return false;}return (b[0] == (byte) && b[1] == (byte) && b[2] == (byte) && b[3] == (byte) && b[4] == (byte) && b[5] == (byte) && b[6] == (byte) && b[7] == (byte) );}private static boolean isBMP(byte[] b) {if (b.length < 2) {return false;}return (b[0] == 0x) && (b[1] == 0x4d);}}

PC获取手机截图、复制文件、安装APK 我在eoe上的帖子的链接PC获取手机截图、复制文件、安装

ORMLite完全解析(四) 官方文档第四章、在Android中使用 官方文档的第四章原标题是UsingWithAndroid,看过前面的文档友友,看到这里可能会有点晕乎,因为从一开始就在介绍ORMLite在Android中的介绍,但是到第四章

android(6) 扇形菜单实现 一.扇形菜单的实现:借鉴了大神们的源码,那我们来看一下扇形菜单是怎么实现的:效果图:主界面布局:RelativeLayoutxmlns:android=

标签: 工具的图

本文链接地址:https://www.jiuchutong.com/biancheng/384300.html 转载请保留说明!

上一篇:工具类(5)Android各版本的兼容方法类(工具类软件有哪些)

下一篇:PC获取手机截图、复制文件、安装APK(手机截取电脑屏幕)

  • 未确认融资费用和长期应付款
  • 2019年个人所得税要补税怎么办
  • 小规模税控盘抵扣增值税报表怎么填
  • 完税证明能作为理赔依据吗
  • 购税盘需要什么东西
  • 现代服务包含哪些服务
  • 去年发生的成本怎么算
  • 原材料进口关税怎么算
  • 车间一般性耗用材料会计分录
  • 一次性预收租金增值税
  • 接受现金捐赠怎么写分录
  • 增值税发票本月没用完可以下月用吗
  • 折扣折让红字发票内容
  • 工程类什么情况下可以三方询价
  • 国税能代开什么费用的发票?
  • 对公提回款是什么意思
  • 上传失败显示网络不佳
  • 在纳税申报时如何填写申报表?
  • 房地产开发成本占比
  • 车辆处置入账价值包括哪些
  • 生产成本为什么不属于费用
  • 跨年度多计提的印花税怎么冲回
  • 软件固定资产还是无形资产
  • 财会报告需要哪些证书
  • 找私人买东西不发货算诈骗吗
  • 离职员工工资退不退
  • 旅客购买电子客票
  • 资产负债表结构是什么
  • linux系统中用户账户有哪些分类
  • win10显示无法连接蓝牙
  • Windows操作系统出现内存错误解决方法
  • 前端登录退出怎么操作
  • 纳税调整增加额怎么做会计分录
  • 商品先入库后得发票如何做账
  • 小微企业城建税及附加减免优惠
  • php常用字符串
  • 酒吧会计要做些什么
  • grad_cam
  • php跳转微信支付
  • vue快速入门与实战开发
  • 股权转让怎么做凭证分录
  • 待转销销项税额是什么
  • rm 删除某个文件
  • 增值税政策执行口径存在的问题及建议
  • 所得税减免与纳税的区别
  • 收到返利冲成本还是记收入
  • python列表排序sorted
  • phpcms 标签
  • 委托收款的含义
  • 偿还银行贷款利息计算
  • 政府奖励收入会计分录
  • 计提的坏账准备计入什么科目
  • 定期定额个体工商户怎么报税
  • 增值税和所得税不一致的说明
  • 以货物抵债的会计分录
  • 摊销费用用什么凭证
  • 用友t3固定资产反结账的操作步骤
  • 无形资产摊销怎么做记账凭证
  • 什么情况需要预缴
  • 无形资产资本化加计扣除可抵扣暂时性差异
  • 计提折旧的固定资产有哪些
  • 收到发票没付款,能打赢官司吗
  • 企业处理原材料会计分录
  • 简述账套管理的主要内容
  • mysql支持的数据类型主要有哪几类
  • mysql密码怎么找回
  • 防止电脑死机
  • mac 鼠标调整
  • linux的samba是什么
  • nodejs oom
  • js datetime
  • 用nodejs做的项目
  • python入门后学什么
  • js文字循环滚动代码
  • js测试工具
  • jquery做下拉
  • Flow之一个新的Javascript静态类型检查器
  • 一般纳税人按季申报的行业
  • 山东省国家地税局官网
  • 红色通知字体
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设