import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.view.Display;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import java.io.InputStream;
public class Get_Image{
public interface ActionListener {
public void pre(Uri uri);
public void finished(Bitmap result_bmp);
public void result_no();
}
public ActionListener getActionListener=null;
private Uri muri;
public Uri get_select_uri(){
return muri;
}
private int img_roataion;
private AppCompatActivity p_activity;
private ActivityResultLauncher<Intent> get_Launcher;
public Bitmap getBitmapbyUri(Uri uri){
Bitmap m_img;
m_img = uri_to_bitmap(p_activity, uri);
if(m_img==null)return null;
Matrix matrix = new Matrix();
matrix.postRotate(img_roataion);
Bitmap rotatedBitmap = Bitmap.createBitmap(m_img, 0, 0, m_img.getWidth(), m_img.getHeight(), matrix, true);
m_img.recycle();
m_img = rotatedBitmap;
return m_img;
}
public Get_Image(AppCompatActivity activity){
muri=null;
img_roataion=0;
p_activity=activity;
get_Launcher = activity.registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
muri=data.getData();
if(getActionListener!=null)getActionListener.pre(muri);
Bitmap m_img;
m_img = uri_to_bitmap(p_activity, muri);
if(m_img!=null && img_roataion!=0) {
Matrix matrix = new Matrix();
matrix.postRotate(img_roataion);
Bitmap rotatedBitmap = Bitmap.createBitmap(m_img, 0, 0, m_img.getWidth(), m_img.getHeight(), matrix, true);
m_img.recycle();
m_img = rotatedBitmap;
}
if(getActionListener!=null)getActionListener.finished(m_img);
}else{
if(getActionListener!=null)getActionListener.result_no();
}
}
});
}
private int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}
private Bitmap uri_to_bitmap(AppCompatActivity activity, Uri uri){
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Bitmap bitmap=null;
ContentResolver cr = activity.getContentResolver();
try {
InputStream inputStream = cr.openInputStream(uri);
BitmapFactory.Options options= getBitmapOptions(1);
options.inJustDecodeBounds=true;
BitmapFactory.decodeStream(inputStream,null,options);
if(options.outWidth>width && options.outHeight>height){
options.inJustDecodeBounds=false;
options.inSampleSize=2;
inputStream = cr.openInputStream(uri);
bitmap = BitmapFactory.decodeStream(inputStream,null,options);
}else{
options.inJustDecodeBounds=false;
options.inSampleSize=1;
inputStream = cr.openInputStream(uri);
bitmap = BitmapFactory.decodeStream(inputStream,null,options);
}
if(Build.VERSION.SDK_INT>=24) {
InputStream in = activity.getContentResolver().openInputStream(uri);
ExifInterface exif = new ExifInterface(in);
int rotation1 = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
img_roataion = exifToDegrees(rotation1);
}
} catch (Exception e) {
}
return bitmap;
}
private BitmapFactory.Options getBitmapOptions(int scale){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = scale;
options.inMutable=true;
return options;
}
public void select_image(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
get_Launcher.launch(intent);
}
}
沒有留言:
張貼留言