Android之Bitmap图片的截屏、模糊处理、传递、使用

项目中遇到了这样一个需求:

当某个条件满足时就截取当前屏幕,并跳转到另外一个页面,同时将这个截屏图片作为下一个页面的背景图片,同时背景图片需要模糊处理

接下来就一步一步解决问题:

1、截取无状态栏的当前屏幕图片,请参考takeScreenShot方法

2、使图片高斯模糊的方法请参考blurBitmap方法

注意:RenderScript是Android在API 11之后加入的,用于高效的图片处理,包括模糊、混合、矩阵卷积计算等

public class ScreenShotUtil {

    // 获取指定Activity的截屏,保存到png文件
    String filenameTemp = "/mnt/sdcard/temp";

    /**
     * takeScreenShot:
     * TODO 截屏    去掉标题栏
     * @param activity
     */
    public static Bitmap takeScreenShot(Activity activity) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        LogHelper.i("TAG", "" + statusBarHeight);

        // 获取屏幕长和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();
        // 去掉标题栏
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }

    /**
     * TODO  用于高效的图片处理,包括模糊、混合、矩阵卷积计算等
     * @param bitmap
     * @param context
     */
    @SuppressLint("NewApi")
    public static Bitmap blurBitmap(Bitmap bitmap, Context context) {

        // Let's create an empty bitmap with the same size of the bitmap we want
        // to blur
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Config.ARGB_8888);

        // Instantiate a new Renderscript
        RenderScript rs = RenderScript.create(context);//RenderScript是Android在API 11之后加入的

        // Create an Intrinsic Blur Script using the Renderscript
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        // Create the Allocations (in/out) with the Renderscript and the in/out
        // bitmaps
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

        // Set the radius of the blur
        blurScript.setRadius(25.f);

        // Perform the Renderscript
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);

        // Copy the final bitmap created by the out Allocation to the outBitmap
        allOut.copyTo(outBitmap);

        // recycle the original bitmap
        bitmap.recycle();

        // After finishing everything, we destroy the Renderscript.
        rs.destroy();

        return outBitmap;

    }
}

3、传递bitmap

刚开始我是这么传递的

bundle.putParcelable("bitmap", ScreenShotUtil.takeScreenShot(theLayout.getActivity()));

继续下面操作:就是将bitmap封装到bundle中,然后封装到intent中启动下一个Activity

ActivityUtil.startActivity(theLayout.getActivity(), LiveEndActivity.class, bundle, false);
/**
     * 开启另外一个activity
     *
     * @param activity
     * @param cls 另外的activity类
     * @param bundle 传递的bundle对象
     * @param isFinish true表示要关闭activity false表示不要关闭activity
     */
    public static void startActivity(Activity activity, Class<?> cls, Bundle bundle, boolean isFinish) {
        Intent intent = new Intent(activity, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        activity.startActivity(intent);
        if (isFinish) {
            activity.finish();
        }
        activity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

然后在LiveEndActivity中这么解析

Bitmap bitmap = intent.getExtras().getParcelable("bitmap");

结果:无法得到预期效果

关键是不报错,debug的时候可以看到我们的确截屏成功,但是Bitmap对象就是没有传递过去,而且不是启动下一个Activity

然后去网上找方法调研

结论:不能直接传递大于40k的图片

解决办法:把bitmap存储为byte数组,然后再继续传递

       Bitmap bitmap = ScreenShotUtil.takeScreenShot(theLayout.getActivity());
       ByteArrayOutputStream baos=new ByteArrayOutputStream();
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
       byte [] bitmapByte =baos.toByteArray();
       bundle.putByteArray("bitmap", bitmapByte);
       // bundle.putParcelable("bitmap", ScreenShotUtil.takeScreenShot(theLayout.getActivity()));

       ActivityUtil.startActivity(theLayout.getActivity(), LiveEndActivity.class, bundle, false);

然后在下一个Activity中这么解析

 byte[] bis =intent.getExtras().getByteArray("bitmap");
            Bitmap bitmap=BitmapFactory.decodeByteArray(bis, 0, bis.length); 

4、假如我们需要将这张图片设置为我们当前Activity的背景图片,我们可以这么做

 if (bitmap != null) {
       bitmap = ScreenShotUtil.blurBitmap(bitmap,getApplicationContext());//高斯模糊处理
       getWindow().getDecorView().setBackgroundDrawable(new BitmapDrawable(bitmap));
 }

问题基本解决了,觉得有用的可以参考一下!

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 02:09:43

Android之Bitmap图片的截屏、模糊处理、传递、使用的相关文章

【Android实战】Bitmap图片的截屏、模糊处理、传递、使用

项目中遇到了这样一个需求: 当某个条件满足时就截取当前屏幕.并跳转到另外一个页面,同一时候将这个截屏图片作为下一个页面的背景图片,同一时候背景图片须要模糊处理 接下来就一步一步解决这个问题: 1.截取无状态栏的当前屏幕图片.请參考takeScreenShot方法 2.使图片高斯模糊的方法请參考blurBitmap方法 注意:RenderScript是Android在API 11之后增加的,用于高效的图片处理,包含模糊.混合.矩阵卷积计算等 public class ScreenShotUtil

Android 7.1.1 系统截屏

frameworks/base/packages/SystemUI/src/com/android/systemui/screenshot/TakeScreenshotService.java TakeScreenshotService.java package com.android.systemui.screenshot; import android.app.Service; import android.content.Intent; import android.os.Handler;

iOS 图片截取 截屏

iOS中对图片的截取 转载的话,请标明出自博客园 截取图片的话 需要指定对象.代码不多,我不太会说,贴一下好了 1 /** 2 * 大图切小图 3 * 4 * @param BIGimg 大图 5 * 6 * @return 小图 7 */ 8 -(UIImage *)getImageFromImage :(UIImage*)BIGimg{ 9 10 //大图bigImage 11 //定义myImageRect,截图的区域 相对位置 12 CGRect myImageRect = CGRect

Android中bitmap图片透明度的处理(以撕美女衣服为例)

原理介绍:将两种不同效果的图片放在相同的位置,改变上面的图片的透明度,就能实现了. 布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_h

Android—将Bitmap图片保存到SD卡目录下或者指定目录

直接上代码就不废话啦 一:保存到SD卡下 [java] view plain copy File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".jpg"); try { out = new FileOutputStream(file); btImage.compress(Bitmap.CompressFormat.JPEG, 90, out); System

Android下免Root权限截屏

/** * 返回的 bitmap就是屏幕的内容 */ private static Bitmap takeScreenShot(Activity activity) { View view = activity.getWindow().getDecorView(); // Enables or disables the drawing cache view.setDrawingCacheEnabled(true); // will draw the view in a bitmap view.b

ios 代码截屏模糊问题解决办法

我们常用的截图方法如下所示: //尺寸是按照 UIGraphicsBeginImageContext(CGSizeMake(100,100 )); //currentView 当前的view 创建一个基于位图的图形上下文并指定大小为 [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文 UIImage *viewImage = UIGraphicsGet

图片合成截屏

-(UIImage *)saveImage:(UIView *)view {     CGRect mainRect = [[UIScreen mainScreen] bounds];         UIGraphicsBeginImageContext(CGSizeMake(320, 200));     CGContextRef context = UIGraphicsGetCurrentContext();     //    [[UIColor blackColor] set];   

Android截屏事件监听

转载注明出处:http://blog.csdn.net/xiaohanluo/article/details/53737655 1. 前言 Android系统没有直接对截屏事件监听的接口,也没有广播,只能自己动手来丰衣足食,一般有三种方法. 利用FileObserver监听某个目录中资源变化情况 利用ContentObserver监听全部资源的变化 监听截屏快捷按键 由于厂商自定义Android系统的多样性,再加上快捷键的不同以及第三方应用,监听截屏快捷键这事基本不靠谱,可以直接忽略. 本文使用