学习了一下压缩和水印,以后要用到的时候可以直接来这里拷贝 
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. kali2.0 系统自带截图功能

    (1)点击左下角的[显示应用程序] (2)在上面搜索栏输入关键字“screen” (3)进入截图选项页面

  2. 区分jquery中的offset和position

    一次又一次地碰到需要获取元素位置的问题, 然后一次又一次地查offset和position的区别. 忍不了了, 这次一定得想办法记下来. position是元素相对于父元素的位置. 这个好记, par ...

  3. 安装安装.net framework过程中出现的问题

    1.安装Microsoft..net framework2.0 SP2 出现 必须使用控制面板中的打开或关闭windows功能,安装或配置.net framework2.0 SP2 原因:可以打开控制 ...

  4. Android开发之实用小知识点汇总-1

    1.去掉android屏幕中的actionbar: this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏 //这个是全屏幕显示的代码 ...

  5. poj 2506 Tiling(递推 大数)

    题目:http://poj.org/problem?id=2506 题解:f[n]=f[n-2]*2+f[n-1],主要是大数的相加; 以前做过了的 #include<stdio.h> # ...

  6. many-to-one和one-to-many的配置比较

    many-to-one配置: <many-to-one name="dailyCatalog" column="daily_catalog_id" cla ...

  7. I.MX6 wm8962 0-001a: DC servo timed out

    /******************************************************************************* * I.MX6 wm8962 0-00 ...

  8. js返回上一页并刷新的多种方法

    js返回上一页并刷新的几种方法.参考链接:http://www.jbxue.com/article/11230.html <a href="javascript:history.go( ...

  9. Android学习系列(15)--App列表之游标ListView(索引ListView)

    游标ListView,提供索引标签,使用户能够快速定位列表项.      也可以叫索引ListView,有的人称也为Tweaked ListView,可能更形象些吧.      一看图啥都懂了: 1. ...

  10. 如何在网页中显示pdf

    用如下的html代码即可(例子): <div class="postBody"> <div id="cnblogs_post_body"> ...