bitmap与file之间转换使用

时间:2023-02-07 07:29:26

1.bitmap与file之间转换使用

2.在三星手机拍照时,图片是反的,所要设置


package com.finstone.face;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;

/**
* 设置bitmap 与 文件
* @author 虞海飞
*
*/
public class BitmapFileSetting {

/**
* 三星手机设置
* 三星手机拍照要旋转
* @param filePath
* @throws IOException
*/
public static File samsungPhoneSetting(String filePath) throws IOException{

//根据图片判断要旋转多少角度
int bitmapDegree = getBitmapDegree(filePath);
//根据图片路径转bitmap
Bitmap bitMBitmap;
bitMBitmap = decodeFile(filePath);
if (bitMBitmap == null){
return null;
}
//旋转后的bitmap
Bitmap rotateBitmapByDegree = rotateBitmapByDegree(bitMBitmap, bitmapDegree);
File saveBitmapFile = saveBitmapFile(rotateBitmapByDegree, filePath);
return saveBitmapFile;
}

/**
* uri 转 File
* @param uri
* @return
*/
public File uriTurnFile(Uri uri, Activity activity){

if(uri == null){
return null;
}

File file = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor actualimagecursor = activity.managedQuery(uri, proj, null,
null, null);
int actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor
.getString(actual_image_column_index);
file = new File(img_path);
return file;
}

/**
* 根据路径 转bitmap
* @param urlpath
* @return
*/
public static Bitmap getBitMBitmap(String urlpath) {

Bitmap map = null;
try {
URL url = new URL(urlpath);
URLConnection conn = url.openConnection();
conn.connect();
InputStream in;
in = conn.getInputStream();
map = BitmapFactory.decodeStream(in);
// TODO Auto-generated catch block
} catch (IOException e) {
e.printStackTrace();
}
return map;
}

/**
* 把batmap 转file
* @param bitmap
* @param filepath
*/
public static File saveBitmapFile(Bitmap bitmap, String filepath){
File file=new File(filepath);//将要保存图片的路径
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}

/**
* 读取图片的旋转的角度
*
* @param path 图片绝对路径
* @return 图片的旋转角度
* @throws IOException
*/
public static int getBitmapDegree(String path) throws IOException {
int degree = 0;
// 从指定路径下读取图片,并获取其EXIF信息
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;
}

return degree;
}

/**
* 将图片按照某个角度进行旋转
*
* @param bm 需要旋转的图片
* @param degree 旋转角度
* @return 旋转后的图片
*/
public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
Bitmap returnBm = null;

// 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(degree);

// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);

if (returnBm == null) {
returnBm = bm;
}
if (bm != returnBm) {
bm.recycle();
}
return returnBm;
}

/**
* 根据 路径 得到 file 得到 bitmap
* @param filePath
* @return
* @throws IOException
*/
public static Bitmap decodeFile(String filePath) throws IOException{
Bitmap b = null;
int IMAGE_MAX_SIZE = 600;

File f = new File(filePath);
if (f == null){
return null;
}
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;

FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();

int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}

//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
return b;
}
}


3.以及调用系统自身的照相机功能,方法

package com.finstone.face;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import com.finstone.utils.PreferencesUtil;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.widget.Toast;

/**
* 调用系统默认的摄像头
* @author yuhaifei
*
*/
@SuppressLint("NewApi")
public class CameraSystem {

/**
*
* @author yuhaifei
*/
public static enum OFTENTYPE {

BEFORECAMERA,//前摄像头
BEHINDCAMERA,
CAMERA_ACTIVITY, //摄像头控制器
ALIPAY
}

private static final String FILE_PATH = "/sdcard/syscamera.jpg";

private Context context;//调用者的
private Activity activity;//调用者的
private Fragment fragment;//调用者的
private String string_Data;//调用者要给activity 回调的数据(默认放的是身份证),给下一个调用者
public void setString_Data(String string_Data) {

//放到缓存里面
PreferencesUtil.setSharedStringData(context, "IdCard", string_Data);
this.string_Data = string_Data;
}

public String getString_Data() {

//从缓存里面拿出来
String sharedStringData = PreferencesUtil.getSharedStringData(context, "IdCard");
return sharedStringData;
}

public CameraSystem() {

}

/**
* 调用系统摄像头
* @param context
* @param activity
*/
public CameraSystem(Context context,Activity activity) {
// TODO Auto-generated constructor stub
this.context = context;
this.activity = activity;

this.upCameraSystem(activity,null);
}

public CameraSystem(Context context,Fragment fragment) {
// TODO Auto-generated constructor stub
this.context = context;
this.fragment = fragment;

this.upCameraSystem(null,fragment);
}

/**
* 启动系统摄像头
*/
private void upCameraSystem(Activity activity,Fragment fragment){

Intent intent = new Intent();
// 指定开启系统相机的Action
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

if (!Build.BRAND.equals("samsung")){

// 调用前置摄像头 OFTENTYPE.BEFORECAMERA.ordinal()
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
}
else {
// 调用前置摄像头 OFTENTYPE.BEFORECAMERA.ordinal()
intent.putExtra("android.intent.extras.CAMERA_FACING", 0);
}

//intent.putExtra("android.intent.extras.CAMERA_FACING_FRONT", 1);
//FindFrontCamera();
intent.addCategory(Intent.CATEGORY_DEFAULT);
// 根据文件地址创建文件
File file = new File(FILE_PATH);
if (file.exists()) {
file.delete();
}

// 把文件地址转换成Uri格式
Uri uri = Uri.fromFile(file);
// 设置系统相机拍摄照片完成后图片文件的存放地址
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
if (activity != null){
activity.startActivityForResult(intent, OFTENTYPE.CAMERA_ACTIVITY.ordinal());
}
else {
fragment.startActivityForResult(intent, OFTENTYPE.CAMERA_ACTIVITY.ordinal());
}
}

/**
* 结束拍照
*/
public File closeCameraSystem(int requestCode){
File file = null;
if (requestCode == OFTENTYPE.CAMERA_ACTIVITY.ordinal()){

//如果不是三星手机
if (!Build.BRAND.equals("samsung")){
file = new File(FILE_PATH);
}

try {
file = BitmapFileSetting.samsungPhoneSetting(FILE_PATH);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Toast.makeText(context, "摄像头调用错误", Toast.LENGTH_LONG).show();
return null;
}
}

return file;
}
}