10.30 alpha
parent
5eb4fb9813
commit
e877ec29cc
@ -0,0 +1,85 @@
|
||||
package com.echo.photo_editor.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Matrix;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Base64;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @author Wang Yuyang
|
||||
* @date 2021-09-22 13:52:43
|
||||
*/
|
||||
public class PhotoLib {
|
||||
public static byte[] BitmapToBytes(Bitmap img) {
|
||||
int bytes = img.getByteCount();
|
||||
ByteBuffer buf = ByteBuffer.allocate(bytes);
|
||||
img.copyPixelsToBuffer(buf);
|
||||
byte[] byteArray = buf.array();
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
public static Bitmap BytesToBitmap(byte[] bytes) {
|
||||
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public static Bitmap Base64ToBitmap(String data) {
|
||||
byte[] decodedString = Base64.decode(data, Base64.DEFAULT);
|
||||
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
|
||||
return decodedByte;
|
||||
}
|
||||
|
||||
public static String saveImageToGallery(Context context, Bitmap bmp) {
|
||||
// 首先保存图片
|
||||
File appDir = context.getFilesDir();
|
||||
if (!appDir.exists()) {
|
||||
appDir.mkdir();
|
||||
}
|
||||
String fileName = System.currentTimeMillis() + ".jpg";
|
||||
File file = new File(appDir, fileName);
|
||||
try {
|
||||
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
|
||||
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
|
||||
fos.flush();
|
||||
fos.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 其次把文件插入到系统图库
|
||||
try {
|
||||
MediaStore.Images.Media.insertImage(context.getContentResolver(),
|
||||
file.getAbsolutePath(), fileName, null);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
String data = MediaStore.Images.Media.insertImage(context.getContentResolver(), bmp, "Colorized Image", ":)");
|
||||
return data;
|
||||
}
|
||||
// 最后通知图库更新
|
||||
// context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
|
||||
public static String getRandomFileName() {
|
||||
return System.currentTimeMillis() + ".jpg";
|
||||
}
|
||||
|
||||
public static Bitmap rotateImage(Bitmap source, float angle) {
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.postRotate(angle);
|
||||
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
|
||||
matrix, true);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue