Android 上传头像(文件)到服务器

深碍√TFBOYSˉ_ 2022-07-18 05:19 390阅读 0赞

现在很多app都用到了头像的功能,我的项目中也用到了。
头像上传分几步:
1.获取头像
2.剪裁头像
3.文件上传
4.服务器的接受保存
首先第一步,无非就是两种方式
1,拍照
2,相册选择
之前的博客中有现到,不重复,链接:
http://blog.csdn.net/w18756901575/article/details/52087242
http://blog.csdn.net/w18756901575/article/details/52085157
第二步,裁剪头像
这一步我用的时鸿洋大神的东西,是个自定的控件,不过我对其中代码进行了一点点的修改,使之更适合自己的项目,里面原理就是监听onTouchEvent来对缩放,双击事件进行相应的处理,裁剪就是根据大小,创建一个新的bitmap,当然说起来挺简单,如果没有别人的代码可以借鉴参考,估计有的搞了,在此谢谢鸿洋大神,下面是鸿洋大神的博客链接:
http://blog.csdn.net/lmj623565791/article/details/39761281
裁剪图片好像还可以调用Android自带的裁剪功能,不过我没用过,
第三步,上传图片,
上传图片不过也就是上传文件,可以将文件转化为byte数组然后进行base64转码后进行上传,在服务器接收到后,将string解码成byte数组,然后在转化为文件。
当然上传的方法有很多,肯定不止这一种 ,因为项目的Android和web接口都是我写的所已我就这样写啦…谁让自己的web技术有限呢,原来写Android的时候一直说调用接口接口,原来自己写了才知道,其实就是一个配置好servlet的java文件.⊙﹏⊙||
写接口和写Android差不过嘛,都是用Java写的,本家嘛
对了文件上传的时候如果从本地读取,会需要读取sd卡的权限,我在6.0的模拟器上测试的时候,一直有问题,找了半天才发现是因为,没有申请权限,,,设立的权限不仅仅时AndroidManifest里面的权限声明,在6.0的系统中需要用代码弹出对话框向用户申请..
贴下请求代码

  1. /**
  2. * 请求权限
  3. */
  4. public void getUserPar() {
  5. // Log.d("测试--授权信息", ContextCompat.checkSelfPermission(this,
  6. // Manifest.permission.WRITE_EXTERNAL_STORAGE) + "");
  7. // Log.d("测试--已授权", PackageManager.PERMISSION_GRANTED + "");
  8. // Log.d("测试--未授权", PackageManager.PERMISSION_DENIED + "");
  9. if (ContextCompat.checkSelfPermission(this,
  10. Manifest.permission.WRITE_EXTERNAL_STORAGE)
  11. == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
  12. Manifest.permission.READ_EXTERNAL_STORAGE)
  13. == PackageManager.PERMISSION_GRANTED) {
  14. } else {
  15. ActivityCompat.requestPermissions(this,
  16. new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
  17. 1);
  18. }
  19. }
  20. @Override
  21. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  22. switch (requestCode) {
  23. case 1: {
  24. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  25. L.d("同意");
  26. } else {
  27. L.d("拒绝");
  28. }
  29. return;
  30. }
  31. }
  32. }

还有文件和数组相互转化的代码

  1. /**
  2. * 将file文件转化为byte数组
  3. *
  4. * @param filePath
  5. * @return
  6. */
  7. public static byte[] getBytes(String filePath) {
  8. byte[] buffer = null;
  9. try {
  10. File file = new File(filePath);
  11. FileInputStream fis = new FileInputStream(file);
  12. ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
  13. byte[] b = new byte[1000];
  14. int n;
  15. while ((n = fis.read(b)) != -1) {
  16. bos.write(b, 0, n);
  17. }
  18. fis.close();
  19. bos.close();
  20. buffer = bos.toByteArray();
  21. } catch (FileNotFoundException e) {
  22. e.printStackTrace();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. return buffer;
  27. }
  28. /**
  29. * 将byte数组转化为file文件
  30. *
  31. * @param bfile
  32. * @param filePath
  33. * @param fileName
  34. */
  35. public static void getFile(byte[] bfile, String filePath, String fileName) {
  36. BufferedOutputStream bos = null;
  37. FileOutputStream fos = null;
  38. File file = null;
  39. try {
  40. File dir = new File(filePath);
  41. if (!dir.exists() && dir.isDirectory()) {
  42. // 判断文件目录是否存在
  43. dir.mkdirs();
  44. }
  45. file = new File(filePath + "\\" + fileName);
  46. fos = new FileOutputStream(file);
  47. bos = new BufferedOutputStream(fos);
  48. bos.write(bfile);
  49. bos.flush();
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. } finally {
  53. if (bos != null) {
  54. try {
  55. bos.close();
  56. } catch (IOException e1) {
  57. e1.printStackTrace();
  58. }
  59. }
  60. if (fos != null) {
  61. try {
  62. fos.close();
  63. } catch (IOException e1) {
  64. e1.printStackTrace();
  65. }
  66. }
  67. }
  68. }

还有base64转码的:

http://blog.csdn.net/w18756901575/article/details/51073847

嗯,好像只有这些,,没了,,end

对了还有圆角,模糊,图片压缩,,,一起放上去吧
20160906163003722

圆形图片效果:

  1. import android.content.Context;
  2. import android.content.res.TypedArray;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Bitmap.Config;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Paint;
  8. import android.graphics.PorterDuff;
  9. import android.graphics.PorterDuffXfermode;
  10. import android.graphics.RectF;
  11. import android.util.AttributeSet;
  12. import android.util.TypedValue;
  13. import android.widget.ImageView;
  14. import com.yesun.league.R;
  15. import com.yesun.league.code.utils.DimensUtils;
  16. /**
  17. * 自定义View,实现圆角,圆形等效果
  18. *
  19. * @author zhy
  20. */
  21. public class CustomImageView extends ImageView {
  22. /**
  23. * TYPE_CIRCLE / TYPE_ROUND
  24. */
  25. private int type;
  26. private static final int TYPE_CIRCLE = 0;
  27. private static final int TYPE_ROUND = 1;
  28. /**
  29. * 图片
  30. */
  31. private Bitmap mSrc;
  32. /**
  33. * 圆角的大小
  34. */
  35. private int mRadius;
  36. /**
  37. * 控件的宽度
  38. */
  39. private int mWidth;
  40. /**
  41. * 控件的高度
  42. */
  43. private int mHeight;
  44. public CustomImageView(Context context, AttributeSet attrs) {
  45. this(context, attrs, 0);
  46. }
  47. public CustomImageView(Context context) {
  48. this(context, null);
  49. }
  50. /**
  51. * 初始化一些自定义的参数
  52. *
  53. * @param context
  54. * @param attrs
  55. * @param defStyle
  56. */
  57. public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
  58. super(context, attrs, defStyle);
  59. TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
  60. R.styleable.CustomImageView, defStyle, 0);
  61. int n = a.getIndexCount();
  62. for (int i = 0; i < n; i++) {
  63. int attr = a.getIndex(i);
  64. switch (attr) {
  65. case R.styleable.CustomImageView_src:
  66. mSrc = BitmapFactory.decodeResource(getResources(),
  67. a.getResourceId(attr, 0));
  68. break;
  69. case R.styleable.CustomImageView_type:
  70. type = a.getInt(attr, 0);// 默认为Circle
  71. break;
  72. case R.styleable.CustomImageView_borderRadius:
  73. mRadius = a.getDimensionPixelSize(attr, (int) TypedValue
  74. .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f,
  75. getResources().getDisplayMetrics()));// 默认为10DP
  76. break;
  77. }
  78. }
  79. a.recycle();
  80. }
  81. @Override
  82. public void setImageBitmap(Bitmap bm) {
  83. super.setImageBitmap(bm);
  84. this.mSrc = bm;
  85. postInvalidate();
  86. }
  87. /**
  88. * 计算控件的高度和宽度
  89. */
  90. @Override
  91. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  92. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  93. /**
  94. * 设置宽度
  95. */
  96. int specMode = MeasureSpec.getMode(widthMeasureSpec);
  97. int specSize = MeasureSpec.getSize(widthMeasureSpec);
  98. if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
  99. {
  100. mWidth = specSize;
  101. } else {
  102. // 由图片决定的宽
  103. int desireByImg = getPaddingLeft() + getPaddingRight()
  104. + mSrc.getWidth();
  105. if (specMode == MeasureSpec.AT_MOST) {
  106. //wrap_content
  107. mWidth = Math.min(desireByImg, specSize);
  108. } else
  109. mWidth = desireByImg;
  110. }
  111. /***
  112. * 设置高度
  113. */
  114. specMode = MeasureSpec.getMode(heightMeasureSpec);
  115. specSize = MeasureSpec.getSize(heightMeasureSpec);
  116. if (specMode == MeasureSpec.EXACTLY) {
  117. // match_parent , accurate
  118. mHeight = specSize;
  119. } else {
  120. int desire = getPaddingTop() + getPaddingBottom()
  121. + mSrc.getHeight();
  122. if (specMode == MeasureSpec.AT_MOST) {
  123. // wrap_content
  124. mHeight = Math.min(desire, specSize);
  125. } else
  126. mHeight = desire;
  127. }
  128. setMeasuredDimension(mWidth, mHeight);
  129. }
  130. /**
  131. * 绘制
  132. */
  133. @Override
  134. protected void onDraw(Canvas canvas) {
  135. switch (type) {
  136. // 如果是TYPE_CIRCLE绘制圆形
  137. case TYPE_CIRCLE:
  138. int min = Math.min(mWidth, mHeight);
  139. /**
  140. * 长度如果不一致,按小的值进行压缩
  141. */
  142. mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false);
  143. canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null);
  144. //下面是绘制圆环的,不喜欢的可以不要
  145. Paint paint=new Paint();
  146. int centre = getWidth()/2; //获取圆心的x坐标
  147. int radius = (int) (centre - DimensUtils.dp2px(getContext(),3)/2); //圆环的半径
  148. paint.setColor(0x88ffffff); //设置圆环的颜色
  149. paint.setStyle(Paint.Style.STROKE); //设置空心
  150. paint.setStrokeWidth(DimensUtils.dp2px(getContext(),3)); //设置圆环的宽度
  151. paint.setAntiAlias(true); //消除锯齿
  152. canvas.drawCircle(centre, centre, radius, paint); //画出圆环
  153. break;
  154. case TYPE_ROUND:
  155. mSrc = Bitmap.createScaledBitmap(mSrc, mWidth, mHeight, false);
  156. canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null);
  157. break;
  158. }
  159. }
  160. /**
  161. * 根据原图和变长绘制圆形图片
  162. *
  163. * @param source
  164. * @param min
  165. * @return
  166. */
  167. private Bitmap createCircleImage(Bitmap source, int min) {
  168. final Paint paint = new Paint();
  169. paint.setAntiAlias(true);
  170. Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);
  171. /**
  172. * 产生一个同样大小的画布
  173. */
  174. Canvas canvas = new Canvas(target);
  175. /**
  176. * 首先绘制圆形
  177. */
  178. canvas.drawCircle(min / 2, min / 2, min / 2, paint);
  179. /**
  180. * 使用SRC_IN,参考上面的说明
  181. */
  182. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//相交部分取后绘制上去的
  183. // paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));//相交部分取
  184. /**
  185. * 绘制图片
  186. */
  187. canvas.drawBitmap(source, 0, 0, paint);
  188. return target;
  189. }
  190. /**
  191. * 根据原图添加圆角
  192. *
  193. * @param source
  194. * @return
  195. */
  196. private Bitmap createRoundConerImage(Bitmap source) {
  197. final Paint paint = new Paint();
  198. paint.setAntiAlias(true);
  199. Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
  200. Canvas canvas = new Canvas(target);
  201. RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());
  202. canvas.drawRoundRect(rect, mRadius, mRadius, paint);
  203. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  204. canvas.drawBitmap(source, 0, 0, paint);
  205. return target;
  206. }
  207. }

压缩,模糊都在这个工具类中,自己看吧:

  1. import android.annotation.TargetApi;
  2. import android.content.Context;
  3. import android.content.res.Resources;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.PixelFormat;
  8. import android.graphics.drawable.BitmapDrawable;
  9. import android.graphics.drawable.Drawable;
  10. import android.os.Build;
  11. import android.renderscript.Allocation;
  12. import android.renderscript.Element;
  13. import android.renderscript.RenderScript;
  14. import android.renderscript.ScriptIntrinsicBlur;
  15. import java.io.ByteArrayInputStream;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. /**
  20. * Created by wkk on 2016/8/23.
  21. * <p>
  22. * 工具类
  23. */
  24. public class BitmapUtils {
  25. // compile 'com.yolanda.nohttp:nohttp:1.0.4'
  26. /**
  27. * 从本地读取图片
  28. *
  29. * @param path
  30. * @return
  31. */
  32. public static Bitmap getBitmapForPath(String path) {
  33. try {
  34. FileInputStream in = new FileInputStream(path);
  35. Bitmap bitmap = BitmapFactory.decodeStream(in);
  36. in.close();
  37. return bitmap;
  38. } catch (Exception e) {
  39. }
  40. return null;
  41. }
  42. /**
  43. * 获取资源文件中的图片
  44. *
  45. * @param context
  46. * @param resourcesId
  47. * @return
  48. */
  49. public static Drawable getDrawableFormResources(Context context, int resourcesId) {
  50. Resources resources = context.getResources();
  51. return new BitmapDrawable(resources, BitmapFactory.decodeResource(resources, resourcesId));
  52. }
  53. /**
  54. * 从资源文件中获取bitmap对象
  55. *
  56. * @param context
  57. * @param resourcesId
  58. * @return
  59. */
  60. public static Bitmap getBitmapFromResources(Context context, int resourcesId) {
  61. return BitmapFactory.decodeResource(context.getResources(), resourcesId);
  62. }
  63. /**
  64. * bitmap转byte数组
  65. *
  66. * @param bitmap
  67. * @return
  68. */
  69. public static byte[] getBitmapbyte(Bitmap bitmap) {
  70. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  71. bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
  72. byte[] datas = baos.toByteArray();
  73. try {
  74. baos.flush();
  75. baos.close();
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. }
  79. return datas;
  80. }
  81. /**
  82. * byte转bitmap数组
  83. *
  84. * @param b
  85. * @return
  86. */
  87. public static Bitmap getBitmaoFrombyte(byte[] b) {
  88. return BitmapFactory.decodeByteArray(b, 0, b.length);
  89. }
  90. /**
  91. * 压缩1
  92. *
  93. * @param srcPath
  94. * @return
  95. */
  96. public static Bitmap getimage(String srcPath) {
  97. BitmapFactory.Options newOpts = new BitmapFactory.Options();
  98. //开始读入图片,此时把options.inJustDecodeBounds 设回true了
  99. newOpts.inJustDecodeBounds = true;
  100. Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空
  101. newOpts.inJustDecodeBounds = false;
  102. int w = newOpts.outWidth;
  103. int h = newOpts.outHeight;
  104. //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
  105. float hh = 800f;//这里设置高度为800f
  106. float ww = 480f;//这里设置宽度为480f
  107. //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
  108. int be = 1;//be=1表示不缩放
  109. if (w > h && w > ww) {
  110. //如果宽度大的话根据宽度固定大小缩放
  111. be = (int) (newOpts.outWidth / ww);
  112. } else if (w < h && h > hh) {
  113. //如果高度高的话根据宽度固定大小缩放
  114. be = (int) (newOpts.outHeight / hh);
  115. }
  116. if (be <= 0)
  117. be = 1;
  118. newOpts.inSampleSize = be;//设置缩放比例
  119. //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
  120. bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
  121. return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
  122. }
  123. /**
  124. * 压缩2
  125. *
  126. * @param image
  127. * @return
  128. */
  129. public static Bitmap comp(Bitmap image) {
  130. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  131. image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  132. if (baos.toByteArray().length / 1024 > 1024) {
  133. //判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
  134. baos.reset();//重置baos即清空baos
  135. image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩50%,把压缩后的数据存放到baos中
  136. }
  137. ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
  138. BitmapFactory.Options newOpts = new BitmapFactory.Options();
  139. //开始读入图片,此时把options.inJustDecodeBounds 设回true了
  140. newOpts.inJustDecodeBounds = true;
  141. Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
  142. newOpts.inJustDecodeBounds = false;
  143. int w = newOpts.outWidth;
  144. int h = newOpts.outHeight;
  145. //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
  146. float hh = 800f;//这里设置高度为800f
  147. float ww = 480f;//这里设置宽度为480f
  148. //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
  149. int be = 1;//be=1表示不缩放
  150. if (w > h && w > ww) {
  151. //如果宽度大的话根据宽度固定大小缩放
  152. be = (int) (newOpts.outWidth / ww);
  153. } else if (w < h && h > hh) {
  154. //如果高度高的话根据宽度固定大小缩放
  155. be = (int) (newOpts.outHeight / hh);
  156. }
  157. if (be <= 0)
  158. be = 1;
  159. newOpts.inSampleSize = be;//设置缩放比例
  160. newOpts.inPreferredConfig = Bitmap.Config.RGB_565;//降低图片从ARGB888到RGB565
  161. //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
  162. isBm = new ByteArrayInputStream(baos.toByteArray());
  163. bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
  164. return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
  165. }
  166. /**
  167. * 质量压缩
  168. *
  169. * @param image
  170. * @return
  171. */
  172. public static Bitmap compressImage(Bitmap image) {
  173. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  174. image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
  175. int options = 100;
  176. while (baos.toByteArray().length / 1024 > 100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩
  177. baos.reset();//重置baos即清空baos
  178. options -= 10;//每次都减少10
  179. image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
  180. }
  181. ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
  182. Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
  183. return bitmap;
  184. }
  185. /**
  186. * 获取图片大小
  187. *
  188. * @param bitmap
  189. * @return
  190. */
  191. public static long getBitmapsize(Bitmap bitmap) {
  192. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
  193. return bitmap.getByteCount();
  194. }
  195. return bitmap.getRowBytes() * bitmap.getHeight();
  196. }
  197. /**
  198. * 对图片进行模糊处理
  199. *
  200. * @param bitmap
  201. * @param context
  202. * @return
  203. */
  204. @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
  205. public static Bitmap blurBitmap(Bitmap bitmap, Context context) {
  206. Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
  207. RenderScript rs = RenderScript.create(context.getApplicationContext());
  208. ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
  209. Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
  210. Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
  211. blurScript.setRadius(25f);
  212. blurScript.setInput(allIn);
  213. blurScript.forEach(allOut);
  214. allOut.copyTo(outBitmap);
  215. bitmap.recycle();
  216. rs.destroy();
  217. return outBitmap;
  218. }
  219. public static Bitmap drawableToBitmap(Drawable drawable) {
  220. Bitmap bitmap = Bitmap.createBitmap(
  221. drawable.getIntrinsicWidth(),
  222. drawable.getIntrinsicHeight(),
  223. drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
  224. Canvas canvas = new Canvas(bitmap);
  225. //canvas.setBitmap(bitmap);
  226. drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
  227. drawable.draw(canvas);
  228. return bitmap;
  229. }
  230. /**
  231. * 水平方向模糊度
  232. */
  233. private static float hRadius = 10;
  234. /**
  235. * 竖直方向模糊度
  236. */
  237. private static float vRadius = 10;
  238. /**
  239. * 模糊迭代度
  240. */
  241. private static int iterations = 7;
  242. private static float a = 1.3f;
  243. /**
  244. * 模糊图片
  245. * @param bmp
  246. * @return
  247. */
  248. public static Drawable BoxBlurFilter(Bitmap bmp) {
  249. hRadius = hRadius * a;
  250. vRadius = vRadius * a;
  251. iterations = (int) (iterations * a);
  252. int width = bmp.getWidth();
  253. int height = bmp.getHeight();
  254. int[] inPixels = new int[width * height];
  255. int[] outPixels = new int[width * height];
  256. Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  257. bmp.getPixels(inPixels, 0, width, 0, 0, width, height);
  258. for (int i = 0; i < iterations; i++) {
  259. blur(inPixels,
  260. outPixels, width, height, hRadius);
  261. blur(outPixels,
  262. inPixels, height, width, vRadius);
  263. }
  264. blurFractional(inPixels,
  265. outPixels, width, height, hRadius);
  266. blurFractional(outPixels,
  267. inPixels, height, width, vRadius);
  268. bitmap.setPixels(inPixels,
  269. 0,
  270. width, 0,
  271. 0,
  272. width, height);
  273. Drawable drawable = new BitmapDrawable(bitmap);
  274. return drawable;
  275. }
  276. public static void blur(int[] in, int[] out, int width, int height, float radius) {
  277. int widthMinus1 = width - 1;
  278. int r = (int) radius;
  279. int tableSize = 2 * r + 1;
  280. int divide[] = new int[256 * tableSize];
  281. for (int i = 0; i < 256 * tableSize; i++)
  282. divide[i] = i / tableSize;
  283. int inIndex = 0;
  284. for (int y = 0; y < height; y++) {
  285. int outIndex = y;
  286. int ta = 0, tr = 0, tg = 0, tb = 0;
  287. for (int i = -r; i <= r; i++) {
  288. int rgb = in[inIndex + clamp(i, 0, width - 1)];
  289. ta += (rgb >> 24) & 0xff;
  290. tr += (rgb >> 16) & 0xff;
  291. tg += (rgb >> 8) & 0xff;
  292. tb += rgb & 0xff;
  293. }
  294. for (int x = 0; x < width; x++) {
  295. out[outIndex] = (divide[ta] << 24) | (divide[tr] << 16) | (divide[tg] << 8)
  296. | divide[tb];
  297. int i1 = x + r + 1;
  298. if (i1 > widthMinus1)
  299. i1 = widthMinus1;
  300. int i2 = x - r;
  301. if (i2 < 0)
  302. i2 = 0;
  303. int rgb1 = in[inIndex + i1];
  304. int rgb2 = in[inIndex + i2];
  305. ta += ((rgb1 >> 24) & 0xff) - ((rgb2 >> 24) & 0xff);
  306. tr += ((rgb1 & 0xff0000) - (rgb2 & 0xff0000)) >> 16;
  307. tg += ((rgb1 & 0xff00) - (rgb2 & 0xff00)) >> 8;
  308. tb += (rgb1 & 0xff) - (rgb2 & 0xff);
  309. outIndex += height;
  310. }
  311. inIndex += width;
  312. }
  313. }
  314. public static void blurFractional(int[] in, int[] out, int width, int height, float radius) {
  315. radius -= (int) radius;
  316. float f = 1.0f / (1 + 2 * radius);
  317. int inIndex = 0;
  318. for (int y = 0; y < height; y++) {
  319. int outIndex = y;
  320. out[outIndex] = in[0];
  321. outIndex += height;
  322. for (int x = 1; x < width - 1; x++) {
  323. int i = inIndex + x;
  324. int rgb1 = in[i - 1];
  325. int rgb2 = in[i];
  326. int rgb3 = in[i + 1];
  327. int a1 = (rgb1 >> 24)
  328. & 0xff;
  329. int r1
  330. = (rgb1 >> 16)
  331. & 0xff;
  332. int g1
  333. = (rgb1 >> 8)
  334. & 0xff;
  335. int b1
  336. = rgb1 & 0xff;
  337. int a2
  338. = (rgb2 >> 24)
  339. & 0xff;
  340. int r2
  341. = (rgb2 >> 16)
  342. & 0xff;
  343. int g2
  344. = (rgb2 >> 8)
  345. & 0xff;
  346. int b2
  347. = rgb2 & 0xff;
  348. int a3
  349. = (rgb3 >> 24)
  350. & 0xff;
  351. int r3
  352. = (rgb3 >> 16)
  353. & 0xff;
  354. int g3
  355. = (rgb3 >> 8)
  356. & 0xff;
  357. int b3
  358. = rgb3 & 0xff;
  359. a1
  360. = a2 + (int)
  361. ((a1 + a3) * radius);
  362. r1
  363. = r2 + (int)
  364. ((r1 + r3) * radius);
  365. g1
  366. = g2 + (int)
  367. ((g1 + g3) * radius);
  368. b1
  369. = b2 + (int)
  370. ((b1 + b3) * radius);
  371. a1
  372. *= f;
  373. r1
  374. *= f;
  375. g1
  376. *= f;
  377. b1
  378. *= f;
  379. out[outIndex]
  380. = (a1 << 24)
  381. | (r1 << 16)
  382. | (g1 << 8)
  383. | b1;
  384. outIndex
  385. += height;
  386. }
  387. out[outIndex]
  388. = in[width - 1];
  389. inIndex
  390. += width;
  391. }
  392. }
  393. public static int clamp(int x,
  394. int a,
  395. int b) {
  396. return (x
  397. < a) ? a : (x > b) ? b : x;
  398. }
  399. }

嗯哼,,真的没了,,end


2017/10/12 08:49
关于上传文件到服务器的.两种思路或者是方法.
1.将文件转成base64编码放在字段里面,像正常的http交互一样去访问服务器上传
2.将文件写入流中上传

发表评论

表情:
评论列表 (有 0 条评论,390人围观)

还没有评论,来说两句吧...

相关阅读