首页 > 代码库 > 屏幕截图
屏幕截图
1 package com.example.printscreen; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import android.app.Activity; 7 import android.graphics.Bitmap; 8 import android.graphics.Rect; 9 import android.view.View; 10 11 public class ScreenShot { 12 // 获取指定Activity的截屏,保存到png文件 13 static Bitmap takeScreenShot(Activity activity) { 14 // View是你需要截图的View 15 View view = activity.getWindow().getDecorView(); 16 view.setDrawingCacheEnabled(true); 17 view.buildDrawingCache(); 18 Bitmap b1 = view.getDrawingCache(); 19 // 获取状态栏高度 20 Rect frame = new Rect(); 21 activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 22 int statusBarHeight = frame.top; 23 System.out.println(statusBarHeight); 24 // 获取屏幕长和高 25 int width = activity.getWindowManager().getDefaultDisplay().getWidth(); 26 int height = activity.getWindowManager().getDefaultDisplay().getHeight(); 27 // 去掉标题栏 28 // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455); 29 Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); 30 view.destroyDrawingCache(); 31 return b; 32 } 33 34 // 保存到sdcard 35 private static void savePic(Bitmap b, String strFileName) { 36 FileOutputStream fos = null; 37 try { 38 fos = new FileOutputStream(strFileName); 39 if (null != fos) { 40 b.compress(Bitmap.CompressFormat.PNG, 90, fos); 41 fos.flush(); 42 fos.close(); 43 } 44 } catch (FileNotFoundException e) { 45 e.printStackTrace(); 46 } catch (IOException e) { 47 e.printStackTrace(); 48 } 49 } 50 }
主Activity :
1 package com.example.printscreen; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.view.View; 5 import android.view.View.OnClickListener; 6 import android.widget.Button; 7 import android.widget.ImageView; 8 import android.graphics.Bitmap; 9 import android.os.Bundle; 10 11 public class MainActivity extends ActionBarActivity { 12 private ImageView imageView; 13 private Button button; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 setupView(); 20 setLis(); 21 22 } 23 private void setLis() { 24 button.setOnClickListener(new OnClickListener() { 25 @Override 26 public void onClick(View v) { 27 // 屏幕截图 28 Bitmap bitmap1 = ScreenShot.takeScreenShot(MainActivity.this); 29 // 初始化模糊工具 30 Bitmap bitmap2 = Blur.fastblur(MainActivity.this, bitmap1, 25); 31 imageView.setImageBitmap(bitmap2); 32 } 33 }); 34 35 } 36 private void setupView() { 37 imageView = (ImageView) findViewById(R.id.imageView); 38 button = (Button) findViewById(R.id.button); 39 } 40 }
模糊处理类:
1 package com.example.printscreen; 2 3 import android.annotation.SuppressLint; 4 import android.content.Context; 5 import android.graphics.Bitmap; 6 import android.os.Build.VERSION; 7 import android.renderscript.Allocation; 8 import android.renderscript.Element; 9 import android.renderscript.RenderScript; 10 import android.renderscript.ScriptIntrinsicBlur; 11 import android.util.Log; 12 13 public class Blur { 14 15 private static final String TAG = "Blur"; 16 17 @SuppressLint("NewApi") 18 public static Bitmap fastblur(Context context, Bitmap sentBitmap, int radius) { 19 20 if (VERSION.SDK_INT > 16) { 21 Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); 22 23 final RenderScript rs = RenderScript.create(context); 24 final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE, 25 Allocation.USAGE_SCRIPT); 26 final Allocation output = Allocation.createTyped(rs, input.getType()); 27 final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); 28 script.setRadius(radius /* e.g. 3.f */); 29 script.setInput(input); 30 script.forEach(output); 31 output.copyTo(bitmap); 32 return bitmap; 33 } 34 35 // Stack Blur v1.0 from 36 // http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html 37 // 38 // Java Author: Mario Klingemann <mario at quasimondo.com> 39 // http://incubator.quasimondo.com 40 // created Feburary 29, 2004 41 // Android port : Yahel Bouaziz <yahel at kayenko.com> 42 // http://www.kayenko.com 43 // ported april 5th, 2012 44 45 // This is a compromise between Gaussian Blur and Box blur 46 // It creates much better looking blurs than Box Blur, but is 47 // 7x faster than my Gaussian Blur implementation. 48 // 49 // I called it Stack Blur because this describes best how this 50 // filter works internally: it creates a kind of moving stack 51 // of colors whilst scanning through the image. Thereby it 52 // just has to add one new block of color to the right side 53 // of the stack and remove the leftmost color. The remaining 54 // colors on the topmost layer of the stack are either added on 55 // or reduced by one, depending on if they are on the right or 56 // on the left side of the stack. 57 // 58 // If you are using this algorithm in your code please add 59 // the following line: 60 // 61 // Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com> 62 63 Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); 64 65 if (radius < 1) { 66 return (null); 67 } 68 69 int w = bitmap.getWidth(); 70 int h = bitmap.getHeight(); 71 72 int[] pix = new int[w * h]; 73 Log.e("pix", w + " " + h + " " + pix.length); 74 bitmap.getPixels(pix, 0, w, 0, 0, w, h); 75 76 int wm = w - 1; 77 int hm = h - 1; 78 int wh = w * h; 79 int div = radius + radius + 1; 80 81 int r[] = new int[wh]; 82 int g[] = new int[wh]; 83 int b[] = new int[wh]; 84 int rsum, gsum, bsum, x, y, i, p, yp, yi, yw; 85 int vmin[] = new int[Math.max(w, h)]; 86 87 int divsum = (div + 1) >> 1; 88 divsum *= divsum; 89 int dv[] = new int[256 * divsum]; 90 for (i = 0; i < 256 * divsum; i++) { 91 dv[i] = (i / divsum); 92 } 93 94 yw = yi = 0; 95 96 int[][] stack = new int[div][3]; 97 int stackpointer; 98 int stackstart; 99 int[] sir; 100 int rbs; 101 int r1 = radius + 1; 102 int routsum, goutsum, boutsum; 103 int rinsum, ginsum, binsum; 104 105 for (y = 0; y < h; y++) { 106 rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; 107 for (i = -radius; i <= radius; i++) { 108 p = pix[yi + Math.min(wm, Math.max(i, 0))]; 109 sir = stack[i + radius]; 110 sir[0] = (p & 0xff0000) >> 16; 111 sir[1] = (p & 0x00ff00) >> 8; 112 sir[2] = (p & 0x0000ff); 113 rbs = r1 - Math.abs(i); 114 rsum += sir[0] * rbs; 115 gsum += sir[1] * rbs; 116 bsum += sir[2] * rbs; 117 if (i > 0) { 118 rinsum += sir[0]; 119 ginsum += sir[1]; 120 binsum += sir[2]; 121 } else { 122 routsum += sir[0]; 123 goutsum += sir[1]; 124 boutsum += sir[2]; 125 } 126 } 127 stackpointer = radius; 128 129 for (x = 0; x < w; x++) { 130 131 r[yi] = dv[rsum]; 132 g[yi] = dv[gsum]; 133 b[yi] = dv[bsum]; 134 135 rsum -= routsum; 136 gsum -= goutsum; 137 bsum -= boutsum; 138 139 stackstart = stackpointer - radius + div; 140 sir = stack[stackstart % div]; 141 142 routsum -= sir[0]; 143 goutsum -= sir[1]; 144 boutsum -= sir[2]; 145 146 if (y == 0) { 147 vmin[x] = Math.min(x + radius + 1, wm); 148 } 149 p = pix[yw + vmin[x]]; 150 151 sir[0] = (p & 0xff0000) >> 16; 152 sir[1] = (p & 0x00ff00) >> 8; 153 sir[2] = (p & 0x0000ff); 154 155 rinsum += sir[0]; 156 ginsum += sir[1]; 157 binsum += sir[2]; 158 159 rsum += rinsum; 160 gsum += ginsum; 161 bsum += binsum; 162 163 stackpointer = (stackpointer + 1) % div; 164 sir = stack[(stackpointer) % div]; 165 166 routsum += sir[0]; 167 goutsum += sir[1]; 168 boutsum += sir[2]; 169 170 rinsum -= sir[0]; 171 ginsum -= sir[1]; 172 binsum -= sir[2]; 173 174 yi++; 175 } 176 yw += w; 177 } 178 for (x = 0; x < w; x++) { 179 rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; 180 yp = -radius * w; 181 for (i = -radius; i <= radius; i++) { 182 yi = Math.max(0, yp) + x; 183 184 sir = stack[i + radius]; 185 186 sir[0] = r[yi]; 187 sir[1] = g[yi]; 188 sir[2] = b[yi]; 189 190 rbs = r1 - Math.abs(i); 191 192 rsum += r[yi] * rbs; 193 gsum += g[yi] * rbs; 194 bsum += b[yi] * rbs; 195 196 if (i > 0) { 197 rinsum += sir[0]; 198 ginsum += sir[1]; 199 binsum += sir[2]; 200 } else { 201 routsum += sir[0]; 202 goutsum += sir[1]; 203 boutsum += sir[2]; 204 } 205 206 if (i < hm) { 207 yp += w; 208 } 209 } 210 yi = x; 211 stackpointer = radius; 212 for (y = 0; y < h; y++) { 213 // Preserve alpha channel: ( 0xff000000 & pix[yi] ) 214 pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum]; 215 216 rsum -= routsum; 217 gsum -= goutsum; 218 bsum -= boutsum; 219 220 stackstart = stackpointer - radius + div; 221 sir = stack[stackstart % div]; 222 223 routsum -= sir[0]; 224 goutsum -= sir[1]; 225 boutsum -= sir[2]; 226 227 if (x == 0) { 228 vmin[y] = Math.min(y + r1, hm) * w; 229 } 230 p = x + vmin[y]; 231 232 sir[0] = r[p]; 233 sir[1] = g[p]; 234 sir[2] = b[p]; 235 236 rinsum += sir[0]; 237 ginsum += sir[1]; 238 binsum += sir[2]; 239 240 rsum += rinsum; 241 gsum += ginsum; 242 bsum += binsum; 243 244 stackpointer = (stackpointer + 1) % div; 245 sir = stack[stackpointer]; 246 247 routsum += sir[0]; 248 goutsum += sir[1]; 249 boutsum += sir[2]; 250 251 rinsum -= sir[0]; 252 ginsum -= sir[1]; 253 binsum -= sir[2]; 254 255 yi += w; 256 } 257 } 258 259 Log.e("pix", w + " " + h + " " + pix.length); 260 bitmap.setPixels(pix, 0, w, 0, 0, w, h); 261 return (bitmap); 262 } 263 264 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。