zoukankan      html  css  js  c++  java
  • android图片的压缩和水印

    学习了一下压缩和水印,以后要用到的时候可以直接来这里拷贝 
    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;
        }
    }
  • 相关阅读:
    Hibernate框架做数据库操作
    Spring AOP几个相关术语复习梳理
    Unknown initial character set index '255' received from server...
    使用IDEA的activiti时报错:发现了以元素 'process' 开头的无效内容...
    获取Class实例的4种方式
    maven设置全局和局部jdk版本
    JSP的9大内置对象和4大域对象(作用域)
    oracle中NVL,NVL2,NULLIF,COALESCE等函数
    MIME类型
    Eclipse集成Tomcat,并发布Web工程
  • 原文地址:https://www.cnblogs.com/jiangbeixiaoqiao/p/5760117.html
Copyright © 2011-2022 走看看