Android图片处理工具类

数据仓库的复用性:设计和构建一个高复用性的数仓 数据仓库的复用性是指在数据仓库的设计和使用过程中,能够实现数据、模型、流程、工具等多个层面的重复利用,减少重复开发,提高开发效率,降低维护成本,并增强灵活性和可扩展性。 阅读详情

// 这个函数会对图片的大小进行判断,并得到合适的缩放比例,比如2即1/2,3即1/3
static int computeSampleSize(BitmapFactory.Options options, int height,
int width) {
int w = options.outWidth;
int h = options.outHeight;
int candidate = (w * h) / (height * width);
if (((w * h) % (height * width)) > 0) {
candidate++;
}
if (candidate == 0)
return 1;
if (true)
;
// Log.i(“ImageTool”, “for w/h ” + w + “/” + h + ” returning ”
// + candidate + “(” + (w / candidate) + ” / ”
// + (h / candidate));
return candidate;
}
public static Bitmap getBitmap(Context context, InputStream is,
int height, int width) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
// 先指定原始大小
options.inSampleSize = 1;
// 只进行大小判断
options.inJustDecodeBounds = true;
// 调用此方法得到options得到图片的大小
is.mark(0);
BitmapFactory.decodeStream(is, null, options);
// 我们的目标是在800pixel的画面上显示。
// 所以需要调用computeSampleSize得到图片缩放的比例
options.inSampleSize = computeSampleSize(options, height, width);
// OK,我们得到了缩放的比例,现在开始正式读入BitMap数据
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
is.reset();
// 根据options参数,减少所需要的内存
Bitmap sourceBitmap = BitmapFactory.decodeStream(is, null, options);
return sourceBitmap;
} catch (OutOfMemoryError error) {
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Bitmap getImageFromStream(Context context, String imagePath,
int height, int width) {
InputStream is;
try {
is = new FileInputStream(imagePath);
return getBitmap(context, is, height, width);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Bitmap getScaleImage(Bitmap bitmap, int resizedWidth,
int resizedHeight, boolean isenlarge, boolean isScale, boolean isFill) {
Bitmap BitmapOrg = bitmap;
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = resizedWidth;
int newHeight = resizedHeight;
if (!isenlarge) {
if (newWidth > width && newHeight > height) {
return bitmap;
}
}
// calculate the scale
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Log.i(“ImageTool”, “width==” + width);
Log.i(“ImageTool”, “height==” + height);
Log.i(“ImageTool”, “resizedWidth==” + resizedWidth);
Log.i(“ImageTool”, “resizedHeight==” + resizedHeight);
Log.i(“ImageTool”, “scaleWidth0==” + scaleWidth);
Log.i(“ImageTool”, “scaleHeight0==” + scaleHeight);
if (!isenlarge) {
if (isScale) {
scaleWidth = scaleWidth < scaleHeight ? scaleWidth
: scaleHeight;
scaleHeight = scaleWidth;
}
} else {
if (isScale) {
scaleWidth = scaleWidth > scaleHeight ? scaleWidth
: scaleHeight;
scaleHeight = scaleWidth;
}
}
Log.i(“ImageTool”, “scaleWidth1==” + scaleWidth);
Log.i(“ImageTool”, “scaleHeight1==” + scaleHeight);
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// if you want to rotate the Bitmap
// matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true);
if (isFill) {
int ShearWidth = 0;
int ShearHeight = 0;
if (resizedBitmap.getWidth() > resizedWidth) {
ShearWidth = resizedBitmap.getWidth() - resizedWidth;
}
if (resizedBitmap.getHeight() > resizedHeight) {
ShearHeight = resizedBitmap.getHeight() - resizedHeight;
}
Bitmap tempBmp = Bitmap.createBitmap(resizedBitmap, ShearWidth / 2, ShearHeight / 2,
resizedBitmap.getWidth() - ShearWidth, resizedBitmap.getHeight() - ShearHeight);
resizedBitmap.recycle();
resizedBitmap = tempBmp;
Log.i(“ImageTool”, “scaleWidth2==” + resizedBitmap.getWidth());
Log.i(“ImageTool”, “scaleHeight3==” + resizedBitmap.getHeight());
}
// make a Drawable from Bitmap to allow to set the Bitmap
// to the ImageView, ImageButton or what ever
return resizedBitmap;
}
/**
* 保存文件
*
* @param bm
* @param fileName
* @throws IOException
*/
public static boolean saveFile(Bitmap bm, String filePath) {
try {
File myCaptureFile = new File(filePath);
if (!myCaptureFile.exists()) {
myCaptureFile.createNewFile();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* 1.放大缩小图片
*
* @param bitmap
* @param w
* @param h
* @return
*/
public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
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);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
matrix, true);
return newbmp;
}
/**
* 2.获得圆角图片的方法
*
* @param bitmap
* @param roundPx
* @return
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
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;
}
/**
* 4.将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_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return bitmap;
}

Jaspersoft Studio安装与配置全攻略:从下载到首个报表生成 本文提供Jaspersoft Studio从下载、安装到创建首个报表的完整实战指南。详细介绍了如何获取安装包、配置工作空间,并通过可视化设计器创建报表模板、添加参数与字段,最终实现数据预览与PDF导出。帮助Java开发者快速掌握这一强大的开源报表工具,提升报表开发效率。 阅读详情

相关推荐

立创开源桌面激光雕刻机DIY全解析:从悬臂结构、Arduino主控到Marlin固件

本文详细解析了基于立创开源平台的桌面激光雕刻机DIY全过程。重点介绍了悬臂式机械结构的设计与优化,阐述了采用Arduino Mega 2560主控与RAMPS 1.4扩展板的经典控制方案,并深入讲解了Marlin固件的关键配置步骤,包括电机步数计算与激光功能启用,为创客和电子爱好者提供了一套完整的从硬件搭建到软件调试的实战指南。

weixin_42510201的博客 383

快速排序算法详解:从核心原理到工程优化实践

排序是数据处理与算法设计中的基础操作,其核心目标是将一组数据按照特定顺序重新排列。基于比较的排序算法通过元素间的比较来决定相对次序,其中分治策略是一种高效的问题解决方法,它将复杂问题分解为结构相似的子问题分别解决。快速排序正是分治思想的经典体现,通过选取基准元素并进行分区操作,实现了平均O(n log n)的时间复杂度,同时具备原地排序的空间优势,常数因子较小,因此在处理随机数据时性能卓越。该算法在数据库索引构建、内存数据排序及编程语言标准库(如C++的std::sort)中有着广泛应用。本文深入剖析了快速

weixin_30634661的博客 353

山景BP10_128DBG开发板通过蓝牙连接手机放歌实验回顾

最近在学习上海山景公司的BP10_128DBG的开发板,因为网上关于BP10开发板的帖子也比较少,在学习的时候也没有太多的参考资料,基本上都是官方的历程。蓝牙放歌这个demo对我来说也比较复杂,特写下此博客进行记录、总结,大家有需要的可以参考。

weixin_56646002的博客 2019

computeSampleSize

转自:http://my.youkuaiyun.com/zljk000/code/detail/18212 computeSampleSize方法,返回一个合适的options.inSampleSize值 Java代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

我的专栏 3571

Verilog实现FIR滤波器

1、FIR滤波器简介 不追究FIR滤波器的深层含义,我们只关注如何实现;可以看出,FIR滤波器的本质就是延迟、系数相乘与求和,如下图: 2、设计 基于以上分析,按照三级流水实现FIR滤波器设计:信号延迟-系数相乘-求和 `timescale 1ns / 1ps module fir( input clk, input rst_n, input [3:0]din, output reg [9:0]dout ); // data reg reg [3:0]din1; reg [3:.

CLL_caicai的博客 1万+

Istio ICA考试之路---2-2

找到dr部分,这部分可以在mirroring里找也可以和题2-1一样在Traffic Management的dr里复制,内容都一样。此题之后都是在cluster-2上操作,所以就不需要再切换了.之后的几题2.1节就忽略掉了。可以看到,80%的流量被镜像到了dev上,实际考试这步可以忽略。看到ex-ip是192.168.126.220。获取gw,把gateways加入到vs配置中。获取istio-ingress的ex-ip。就获得了这样一个模板文件。查看2个pod的日志。

Q先生 854

Android如何最优化的读取一张Bitmap

Drawable缓存问题,显示清晰度, Resource.getDrawable会根据ID返回Drawable,然后把这个Drawable缓存起来,看以下这个代码段: InputStream is = mAssets.openNonAsset( value.assetCookie, file, AssetManager.ACC

农场老马的专栏 8850

【硬件】LT3763中文手册

1)精确控制输入和输出电流;2) 3000:1真彩色PWM™调光;3) ±1.5%电压调节精度;4) ±6%电流调节精度;5)6V至60 V输入电压范围;6)宽输出范围最高可达55V;7)<2µA关断电流;8)用于负载电流热控制的控制引脚;9)输入和输出电流监控和限制;10)开路、短路和C/10故障检测;11)适用于LED应用的PWM驱动器输出;12)热增强型28引脚FE封装。

qq_39200110的博客 489

BitmapFactory.Options详解

public Bitmap inBitmap If set, decode methods that take the Options object will attempt to reuse this bitmap when loading content. public int inDensity The pixel

luohai859的专栏 2万+

揭秘AI人工智能中Stable Diffusion的模型微调技巧

Stable Diffusion作为一种强大的文本到图像生成模型,在艺术创作、设计、娱乐等多个领域展现出了巨大的应用潜力。然而,其默认的模型输出可能无法满足特定用户或特定场景的个性化需求。本文章的目的在于深入揭秘Stable Diffusion的模型微调技巧,让读者能够根据自己的需求对模型进行定制化调整,生成符合特定风格、主题或要求的图像。范围涵盖了从基础概念到实际操作,从理论原理到项目实战的全方位内容,旨在为读者提供一个系统且深入的学习指南。

大模型应用工坊 948

OpenHarmony开发一个App,安装到BearPi-HM Micro开发板

建议先阅读我们这篇,这里面详细介绍了,以及hap包安装的全过程。本篇是介绍如何开发一个App,然后安装到开发板上,比如hap包,无法安装等问题。

Halifax 1030

java.lang.OutOfMemoryError: bitmap size exceeds VM budget解决方法

BitmapFactory.decodeFile(imageFile); 用BitmapFactory解码一张图片时,有时会遇到该错误。这往往是由于图片过大造成的。要想正常使用,则需要分配更少的内存空间来存储。 BitmapFactory.Options.inSampleSize 设置恰当的inSampleSize可以使BitmapFactory分配更少的空间以消除该错误。inS...

weixin_30326741的博客 267

【施磊】C++课程笔记目录

【C++编程】施磊老师C++笔记目录

GeniusAng的博客 4122

Android 图片处理工具类封装2

http://www.2cto.com/kf/201312/263638.html Android 图片处理工具类封装 2013-12-10     0 个评论   来源:Wiker Yong 的专栏   收藏  我要投稿 android 图片处理工具类封装,包含保存图片、 写文件到SD卡,文件转Bitmap、缩放,给图片加倒影等强大功能 ?

haocaige 703

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

android 图片处理工具类

图片工具类,可用于Bitmap, byte array, Drawable之间进行转换以及图片缩放,目前功能薄弱,后面会进行增强。如: bitmapToDrawable(Bitmap b) bimap转换为drawable drawableToBitmap(Drawable d) drawable转换为bitmap drawableToByte(Drawable d) drawable转换为

hhlin1的专栏 1202

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Android开发:图片处理工具类

import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuf...

小五博客园 421

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

android把图片保存到本地工具类,Android中图片处理(压缩 保存 显示 截屏 视图View转为图片Bitmap)函数工具类.doc...

Android中图片处理(压缩 保存 显示 截屏 视图View转为图片Bitmap)函数工具类示例:void shareShotScreen() {LinearLayout layout = (LinearLayout) findViewById(R.id.plan_layout);Bitmap bitmap = ImageUtils.convertBitmap(layout);String pa...

weixin_42286789的博客 174

安卓开发图片处理工具类

代码如下: import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.net....

qq_30239261的博客 756

Android 图片处理工具类封装

android 图片处理工具类封装,包含保存图片、 写文件到SD卡,文件转Bitmap、缩放,给图片加倒影等强大功能 import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import

猫叔的专栏 4249

android 图片处理工具类封装,Android图片处理工具类(持续积累补充)

packagecn.mucang.android.community.utils;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Matrix;importjava.io.ByteArrayOutputStream;importjava.io.File;i...

weixin_39651488的博客 164

Android 图片处理工具类汇总

很有用的Android图片处理工具,实现各种图片处理效果

shell812的专栏 2838

android 图片工具类,Android图片处理工具类

packagecom.zhy.weather.util;importandroid.graphics.Bitmap;importandroid.graphics.Canvas;importandroid.graphics.LinearGradient;importandroid.graphics.Matrix;importandroid.graphics.Paint;importandroid.g...

weixin_29053695的博客 232

android 图片处理 工具类,图片处理工具类(缩放,圆角,倒影)

[android]代码库package com.demo;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.Canvas;import android.graphics.LinearGradient;import android.graphics.Matrix;i...

weixin_39707851的博客 116

android图片处理工具类

/**  * 图片工具类  *   *   * @author minner  *  */ public class ImageUtils { // 将Drawbale图片转换为Bitmap位图 // Resources resources = getResources(); // Bitmap bitmap = BitmapFactory.decodeResource(r

470

Android中图片处理(压缩 保存 显示 截屏 视图View转为图片Bitmap)函数工具类

Android中图片处理(压缩 保存  显示 截屏  视图转为图片)函数工具类 调用示例: void shareShotScreen() { LinearLayout layout = (LinearLayout) findViewById(R.id.plan_layout); Bitmap bitmap = ImageUtils.convertBitmap(layout); Strin

zhanghao_Hulk的专栏 3253

android 图片工具类,android ImageUtils 图片处理工具类

/*** 添加文字到图片,类似水印文字。* @param gContext* @param gResId* @param gText* @return*/public static Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {Resources resources = gContext.getResour...

weixin_29185167的博客 194
上一篇: 安卓的几种延迟方式
下一篇: 判断用于使用哪个渠道进行联网
yang0range
博客等级 码龄12年 15粉丝 28原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值