根据本地相片路径上传图片到服务器的2中方法
2015-07-25 13:36
387 查看
package com.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;
/**
* 根据本地相片路径上传图片到服务器的2中方法
* @author yjbo
*2015-07-25 13:37:14
*/
public class PictureUtil {
/**
* 上传图片时:把bitmap转换成String
* 这2种方法有优缺点:
* 1)法一(控制长宽的):这个正常拍的1-2M的照片只会压缩到400kb左右,但是画质也的确清楚;然后拍完照也不会黑屏;
* 2)法二(控制大小(kb字节的)):这个就是控制照片大小在100kb以内,不改变照片原来的长宽,然后拍完照会黑屏(应该是循环次数多了);相对而言除了这种方案不好,服务器中不是很清楚
* @param filePath
* @return
*/
public static String bitmapToString(String filePath) {
Bitmap bitmap ;
// filePathOri = filePath;
// ==图片路径==/storage/sdcard0/DCIM/Camera/IMG_20141119_122410p1.jpg
// System.out.println("==图片路径=="+filePathOri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//法一:这是控制图片的长宽的:
Bitmap bm = getSmallBitmap(filePath);
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//法二:压缩图片:这是控制图片的大小的(100kb以内):
try {
BitmapFactory.Options options = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(filePath, options);
int options1 = 100;
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (baos.toByteArray().length / 1024 > 100) {//原图都是基本1-2M;
if(options1<10){//大约公约数的2倍
break;//这是因为有可能options1已经为0了,但是还没到100kb以内
}
baos.reset();
options1 -= 5;
bitmap.compress(Bitmap.CompressFormat.JPEG, options1, baos);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}
/**
* 根据路径获得突破并压缩返回bitmap用于显示
*
* @param imagesrc
* @return
*/
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
/**
* 计算图片的缩放�?
*
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (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.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
/**
* 根据路径删除图片
*
* @param path
*/
public static void deleteTempFile(String path) {
File file = new File(path);
if (file.exists()) {
file.delete();
}
}
/**
* 添加到图�?
*/
public static void galleryAddPic(Context context, String path) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(path);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
/**
* 获取保存图片的目�?
*
* @return
*/
public static File getAlbumDir() {
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), getAlbumName());
if (!dir.exists()) {
dir.mkdirs();
}
return dir;
}
/**
* 获取保存 隐患�?��的图片文件夹名称
*
* @return
*/
public static String getAlbumName() {
return "sheguantong";
// ==图片路径==/storage/sdcard0/DCIM/Camera/IMG_20141119_122410p1.jpg
// String filepathPp = null;
// String filePathOri2 = filePathOri.substring(19, 25);
//
// if(filePathOri.contains("p0")){
// filepathPp= filePathOri2.replace("p0.jpg", "pic1_1");
// }else if(filePathOri.contains("p1")){
// filepathPp= filePathOri2.replace("p1.jpg", "pic2_1");
// }else if(filePathOri.contains("p2")){
// filepathPp= filePathOri2.replace("p2.jpg", "pic3_1");
// }else if(filePathOri.contains("p3")){
// filepathPp= filePathOri2.replace("p3.jpg", "pic4_1");
// }
// return ;
}
}
import java.io.ByteArrayOutputStream;
import java.io.File;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;
/**
* 根据本地相片路径上传图片到服务器的2中方法
* @author yjbo
*2015-07-25 13:37:14
*/
public class PictureUtil {
/**
* 上传图片时:把bitmap转换成String
* 这2种方法有优缺点:
* 1)法一(控制长宽的):这个正常拍的1-2M的照片只会压缩到400kb左右,但是画质也的确清楚;然后拍完照也不会黑屏;
* 2)法二(控制大小(kb字节的)):这个就是控制照片大小在100kb以内,不改变照片原来的长宽,然后拍完照会黑屏(应该是循环次数多了);相对而言除了这种方案不好,服务器中不是很清楚
* @param filePath
* @return
*/
public static String bitmapToString(String filePath) {
Bitmap bitmap ;
// filePathOri = filePath;
// ==图片路径==/storage/sdcard0/DCIM/Camera/IMG_20141119_122410p1.jpg
// System.out.println("==图片路径=="+filePathOri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//法一:这是控制图片的长宽的:
Bitmap bm = getSmallBitmap(filePath);
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//法二:压缩图片:这是控制图片的大小的(100kb以内):
try {
BitmapFactory.Options options = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(filePath, options);
int options1 = 100;
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (baos.toByteArray().length / 1024 > 100) {//原图都是基本1-2M;
if(options1<10){//大约公约数的2倍
break;//这是因为有可能options1已经为0了,但是还没到100kb以内
}
baos.reset();
options1 -= 5;
bitmap.compress(Bitmap.CompressFormat.JPEG, options1, baos);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}
/**
* 根据路径获得突破并压缩返回bitmap用于显示
*
* @param imagesrc
* @return
*/
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
/**
* 计算图片的缩放�?
*
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (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.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
/**
* 根据路径删除图片
*
* @param path
*/
public static void deleteTempFile(String path) {
File file = new File(path);
if (file.exists()) {
file.delete();
}
}
/**
* 添加到图�?
*/
public static void galleryAddPic(Context context, String path) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(path);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
/**
* 获取保存图片的目�?
*
* @return
*/
public static File getAlbumDir() {
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), getAlbumName());
if (!dir.exists()) {
dir.mkdirs();
}
return dir;
}
/**
* 获取保存 隐患�?��的图片文件夹名称
*
* @return
*/
public static String getAlbumName() {
return "sheguantong";
// ==图片路径==/storage/sdcard0/DCIM/Camera/IMG_20141119_122410p1.jpg
// String filepathPp = null;
// String filePathOri2 = filePathOri.substring(19, 25);
//
// if(filePathOri.contains("p0")){
// filepathPp= filePathOri2.replace("p0.jpg", "pic1_1");
// }else if(filePathOri.contains("p1")){
// filepathPp= filePathOri2.replace("p1.jpg", "pic2_1");
// }else if(filePathOri.contains("p2")){
// filepathPp= filePathOri2.replace("p2.jpg", "pic3_1");
// }else if(filePathOri.contains("p3")){
// filepathPp= filePathOri2.replace("p3.jpg", "pic4_1");
// }
// return ;
}
}
相关文章推荐
- HTML之表格与表单
- UVa 11400 - Lighting System Design(DP)
- [程序设计语言] 堆和栈的全面总结
- 使用 promise.then + 递归的方法异步转同步
- QTP关闭所有的应用程序窗口,场景恢复
- 转载是一种凝聚(之一) 国内外程序员生态圈对比
- Android中设置自己软件的铃声+震动
- ACM两个士兵打牌
- 转载是一种凝聚(之一) 国内外程序员生态圈对比
- PHP验证登录状态和安全性
- 关于hkcmd.exe造成的和Eclipse之间热键冲突
- android中的后退键——onBackPressed()的使用
- wamp开机自动启动
- 小白学开发(iOS)OC_组合模式(2015-07-24)
- MATLAB中的ind2vec和vec2ind函数
- openSAP中国新平台的介绍
- 一次Maven项目编译失败的分析过程
- 网站的安全性测试工具网站
- 设计模式汇总
- SAP创建中国版免费在线课程openSAP