安卓简单图片处理程序
来源:互联网 发布:淘宝童装店铺起名 编辑:程序博客网 时间:2024/05/16 08:42
接上一篇博客,在图片上传下载过程中,常常需要对图片进行一些简单的处理,例如压缩,调正,转换形式等等
现介绍一些简单的处理程序
import android.content.Context;
import android.graphics.Bitmap;import android.graphics.Bitmap.CompressFormat;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.graphics.drawable.BitmapDrawable;import android.media.ExifInterface;import android.util.Log;import android.widget.ImageView;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Date;/** * Created by user on 2016/10/7. */public class UploadPhotoUtil<MultipartEntity> { final static int BUFFER_SIZE = 1024; public static UploadPhotoUtil uploadPhotoUtil; public static UploadPhotoUtil getInstance() { if (uploadPhotoUtil == null) { uploadPhotoUtil = new UploadPhotoUtil(); } return uploadPhotoUtil; } public String getFileType(String fileName) { return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); } public String getUploadBitmapZoomString(String filePath) { String fileString = ""; try { Bitmap bitmap = trasformToZoomBitmapAndLessMemory(filePath); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 100, bos); byte[] result = bos.toByteArray(); fileString = new String(result, "ISO-8859-1"); } catch (Exception e) { e.printStackTrace(); } return fileString; } public static byte[] InputStreamTOByteArray(InputStream in) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); int count; byte[] data = new byte[BUFFER_SIZE]; while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) { outStream.write(data, 0, count); } outStream.close(); return outStream.toByteArray(); } /* 如果有些手机拍照反转,读取角度*/ public int readPictureDegree(String path){ int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; } /* 如果有些手机拍照反转,那么把图片调正 */ public Bitmap rotaingImageView(int angle, Bitmap bitmap) { Matrix matrix = new Matrix(); matrix.postRotate(angle); System.out.println("angle2=" + angle); Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true); return resizedBitmap; } public Bitmap trasformToZoomBitmapAndLessMemory(String url) { File file = new File(url); Log.d("logout", "file.length()---------------------" + file.length()); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; int inSampleSize = 1; if (file.length() < 256 * 1024) { options.inPreferredConfig = Bitmap.Config.RGB_565; } else if (file.length() < 512 * 1024) { options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 2; inSampleSize = 2; } else if (file.length() < 1024 * 1024) { options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 4; inSampleSize = 4; } else { options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 6; inSampleSize = 6; } //options.inPurgeable = true; //options.inInputShareable = true; options.inJustDecodeBounds = false; Log.d("gaolei", "inSampleSize-----------------" + inSampleSize); int degree = readPictureDegree(file.getAbsolutePath()); InputStream is = null; try { is = new FileInputStream(url); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap cameraBitmap = BitmapFactory.decodeStream(is, null, options); // Bitmap cameraBitmap = BitmapFactory.decodeFile(url, options); Bitmap photo = rotaingImageView(degree, cameraBitmap); if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return photo; } //注意显示本地图片时一定要压缩质量,不然容易出现OOM public Bitmap trasformToZoomPhotoAndLessMemory(String url) { File file = new File(url); Log.d("gaolei", "file.length()--------original-------------" + file.length()); BitmapFactory.Options options = new BitmapFactory.Options(); // 通过这个bitmap获取图片的宽和高 options.inJustDecodeBounds = true; int inSampleSize = 1; if (file.length() < 256 * 1024) { options.inPreferredConfig = Bitmap.Config.ARGB_8888; } else if (file.length() < 512 * 1024) { options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 2; inSampleSize = 2; } else if (file.length() < 1024 * 1024) { options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 4; inSampleSize = 4; } else { options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 6; inSampleSize = 6; } //options.inPurgeable = true; // options.inInputShareable = true; // 注意这次要把options.inJustDecodeBounds 设为 false,这次图片是要读取出来的 options.inJustDecodeBounds = false; Log.d("gaolei", "inSampleSize-----------------" + inSampleSize); int degree = readPictureDegree(file.getAbsolutePath()); // Log.d("gaolei", "degree------------uploadImg--------------" + // degree); InputStream is = null; try { is = new FileInputStream(url); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap cameraBitmap = BitmapFactory.decodeStream(is, null, options); // Bitmap cameraBitmap = BitmapFactory.decodeFile(url, options); Bitmap photo = rotaingImageView(degree, cameraBitmap); if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return photo; } public Bitmap trasformToZoomBitmapInHeightAndWidth(Context context, String url) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; options.inJustDecodeBounds = true; BitmapFactory.decodeFile(url, options); options.inSampleSize = calculateInSampleSize(options, CommonUtils .getUtilInstance().dp2px(context, 80), CommonUtils .getUtilInstance().dp2px(context, 80)); // options.inPurgeable = true; //options.inInputShareable = true; options.inJustDecodeBounds = false; File file = new File(url); int degree = readPictureDegree(file.getAbsolutePath()); InputStream is = null; try { is = new FileInputStream(url); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap cameraBitmap = BitmapFactory.decodeStream(is, null, options); // Bitmap cameraBitmap = BitmapFactory.decodeFile(url, options); Bitmap photo = rotaingImageView(degree, cameraBitmap); if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return photo; } public void setImageDrawable(Context context, ImageView imageView, int drawable) { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.RGB_565; //opt.inPurgeable = true; // opt.inInputShareable = true; InputStream is = context.getResources().openRawResource(drawable); Bitmap bitmap = BitmapFactory.decodeStream(is, null, opt); try { is.close(); } catch (IOException e) { e.printStackTrace(); } BitmapDrawable bd = new BitmapDrawable(context.getResources(), bitmap); imageView.setImageDrawable(bd); } public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = (int) Math.floor(height / reqHeight); final int widthRatio = (int) Math.floor(width / reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; }}
阅读全文
0 0
- 安卓简单图片处理程序
- 安卓 图片处理
- 安卓图片处理技术
- java 写的简单处理图片程序
- 安卓笔记之网络图片查看器小程序以及消息处理机制运用
- 安卓处理Html----图片异步加载
- 安卓图片处理内存溢出问题
- 自动处理安卓不同尺寸图片
- 安卓 .9图片处理讲解
- 安卓圆形图片处理方法
- 安卓图片Bitmap一些旋转处理
- 安卓图片处理出现内存溢出
- 安卓上传服务器图片处理
- 关于安卓图片的处理
- 安卓开发_简单图片浏览器
- 简单安卓图片自动轮播
- 安卓案例-简单图片查看器
- 简单的安卓手势处理类
- EJB学习笔记_8_SessionBean生命周期
- php缓冲区详解
- VBA创建excel工作薄
- youtube爬虫搭建
- [arm驱动]linux等待队列阻塞中断IO的应用
- 安卓简单图片处理程序
- Django+uwsgi+Nginx 踩坑
- 号称全球最快电动摩托车 限量50台价格不菲
- 杭电oj-An Easy Task
- ecshop警告
- 静态代理、JDK与CGLIB动态代理、AOP+IoC
- 聊天机器人 AIML文件 <bot>标签解释(四)
- iOS每日一记之———————————————JS调用OC的代码(客户端实现)
- Hibernate 查询MatchMode的四种模式