学习了一下压缩和水印,以后要用到的时候可以直接来这里拷贝 
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bitmap_compress.MainActivity" > <ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="300sp"
android:contentDescription="@null"
android:src="@drawable/a" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> </LinearLayout>

MainActivity.Java

package com.example.bitmap_compress;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView imageView;
private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//图片压缩
imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = BitmapCompressTools.decodeSampledBitmapFromResource(
getResources(), R.drawable.a, 100, 100);
Log.i("Original bytes",
"----->>>"
+ BitmapFactory.decodeResource(getResources(),
R.drawable.a).getByteCount());
Log.i("Compressed bytes", "----->>>" + bitmap.getByteCount());
imageView.setImageBitmap(bitmap); //水印,其实就是往图片上写字
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = BitmapTools.createBitmap(getResources(),
R.drawable.a, "a.png");
imageView.setImageBitmap(bitmap);
}
});
}
}

然后给出每个的工具类 
BitmapTools.java

package com.example.bitmap_compress;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream; import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Environment; public class BitmapTools { public static Bitmap createBitmap(Resources resources, int resid, String name) {
Bitmap bitmap = BitmapFactory.decodeResource(resources, resid);
// 复制一份新的Bitmap,因为不能直接在原有的bitmap上进行水印操作
// Bitmap.config存储的格式
Bitmap newBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
// 使用自定义画布
Canvas canvas = new Canvas(newBitmap);
Paint paint = new Paint();
paint.setTextSize(200);
canvas.drawText("hello", 100, 100, paint);
// 判断SDcard是否在可用状态
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 直接将图片保存在根目录下
File root = Environment.getExternalStorageDirectory();
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(new File(root, name));
// 对图片进行压缩并以png格式,保存在sdcard中
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
} catch (Exception e) {
}
}
return newBitmap;
}
}

BitmapCompressTools.java

package com.example.bitmap_compress;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; public class BitmapCompressTools { public static Bitmap decodeSampledBitmapFromResource(Resources resources,
int resId, int width, int height) {
// 给定的BitmapFactory设置解码的参数
BitmapFactory.Options options = new BitmapFactory.Options();
// 从解码器中获得原始图片的宽高,而避免申请内存空间
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, resId, options);
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(resources, resId, options);
} /**
* 指定输出图片的缩放比例
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// 获得原始图片的宽高
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
int inSimpleSize = 1;
if (imageHeight > reqHeight || imageWidth > reqWidth) { // 计算压缩的比例:分为宽高比例
final int heightRatio = Math.round((float) imageHeight
/ (float) reqHeight);
final int widthRatio = Math.round((float) imageWidth
/ (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSimpleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSimpleSize;
}
}

android图片的压缩和水印的更多相关文章

  1. Android图片处理——压缩、剪裁、圆角、保存

    点击查看原文 项目中用到的关于图片的处理 public class UtilPicture { public static final String IMAGE_UNSPECIFIED = " ...

  2. Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案

    1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...

  3. Android图片压缩方法总结

    本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩).   第一:质量压缩方法:   ? 1 2 3 ...

  4. android图片压缩方法

    android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...

  5. Android之获取本地图片并压缩方法

    这两天在做项目时,做到上传图片功能一块时,碰到两个问题,一个是如何获取所选图片的路径,一个是如何压缩图片,在查了一些资料和看了别人写的后总算折腾出来了,在此记录一下. 首先既然要选择图片,我们就先要获 ...

  6. android图片压缩的3种方法实例

    android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...

  7. Android实现图片的压缩、旋转工具类

    import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matri ...

  8. 性能优化——Android图片压缩与优化的几种方式

    图片优化压缩方式大概可以分为以下几类:更换图片格式,质量压缩,采样率压缩,缩放压缩,调用jpeg压缩等1.设置图片格式Android目前常用的图片格式有png,jpeg和webp,png:无损压缩图片 ...

  9. android图片压缩总结

    一.bitmap 图片格式介绍 android中图片是以bitmap形式存在的,那么bitmap所占内存,直接影响到了应用所占内存大小,首先要知道bitmap所占内存大小计算方式: bitmap内存大 ...

随机推荐

  1. CAS SiteMinder (单点登录)

    http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

  2. 1414. Astronomical Database(STL)

    1414 破题 又逼着用stl 卡内存 trie树太耗了 水不过去 用set存字符串 set可以自己按一定顺序存 且没有重复的 再用lower_bound二分查找字符串的第一次出现 接着往后找就行了 ...

  3. java之内部类与匿名内部类

    Java 内部类 分四种:成员内部类.局部内部类.静态内部类和匿名内部类. 1.成员内部类: 即作为外部类的一个成员存在,与外部类的属性.方法并列. 注意:成员内部类中不能定义静态变量,但可以访问外部 ...

  4. 用纯css写出三角形

    1.新建一个元素,随便什么元素,不过我习惯性的会用块元素来做.如果行内元素就display:block它.<div class="triangle"></div& ...

  5. Ejabberd源码解析前奏--集群

    一.如何工作 一个XMPP域是由一个或多个ejabberd节点伺服的. 这些节点可能运行在通过网络连接的不同机器上. 它们都必须有能力连接到所有其它节点的4369端口, 并且必须有相同的 magic ...

  6. 结构型:代理模式 Vs 适配器模式 Vs 门面模式(外观模式)

    先上UML图 代理模式: 适配器模式: 门面模式(外观模式): 打了例子……呃……举个比方 代理模式: 水浒街,西门庆看上潘金莲,想和她嘿咻嘿咻,但是自己有不能去找潘金莲去说,于是他找到了金牌代理人王 ...

  7. 【Python】python-一个class继承的小case

    #-*- coding:utf-8 -*-#定义银行类,包含属性:用户名,账户,余额:包含方法有:查询余额,存钱,取钱class BankAccount(): def __init__(self,na ...

  8. 017QTP 描述性编程的使用方法

    一.什么时候使用描述性编程 在测试过程中,有些界面元素是动态出现或动态变化的,在录制的时候并没有添加到对象库中 二.描述性编程的运行原理 用描述性编程编写的测试脚本在运行时,QTP会使用测试脚本中给出 ...

  9. 问题:贴友关于CSS效果的实现

    今日在百度贴吧中,一贴有提出如下问题: 对于这个问题,咱们贴上代码看效果 1: <html> 2: <head> 3: <meta http-equiv="co ...

  10. Python:itertools模块

    itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用. ch ...