android 图像处理

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};


    private Button mButton;
    private ImageView mImage;
    private String TAG = "MainActivity";
    private File mFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//6.0以后权限申请
 verifyStoragePermissions(MainActivity.this);
        mImage = (ImageView) findViewById(R.id.image);
        mButton = findViewById(R.id.button);
//将图片存入sd卡里
 mFile = new File(Environment.getExternalStorageDirectory() + "/abc.jpg");
        Bitmap bitmap1 = BitmapFactory.decodeFile(mFile.getAbsolutePath());
        mImage.setImageBitmap(bitmap1);
        Log.d(TAG, "剪裁前:" + bitmap1.getByteCount());

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap bitmap2 = BitMapUtil.ratio(mFile.getAbsolutePath(), mImage.getWidth(), mImage.getHeight());
                Log.d(TAG, "剪裁后:" + bitmap2.getByteCount());
                mImage.setImageBitmap(bitmap2);
            }
        });
    }

    public static void verifyStoragePermissions(Activity activity) {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限,去申请写的权限,会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yl.mybitmaptest.MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_margin="20dp" />


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:text="加载图片" />

</RelativeLayout>

public class BitMapUtil {
    public static Bitmap ratio(String filePath, int pixelW, int pixelH) {
        BitmapFactory.Options newOptions = new BitmapFactory.Options();
        newOptions.inJustDecodeBounds = true;
        newOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        //预加载
        BitmapFactory.decodeFile(filePath, newOptions);
        int originalH = newOptions.outHeight;
        int originalW = newOptions.outWidth;
        newOptions.inSampleSize = getSimpleSize(originalW, originalH, pixelW, pixelH);
        newOptions.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, newOptions);
    }

    private static int getSimpleSize(int originalW, int originalH, int pixelW, int pixelH) {
        int simpleSize = 1;
        if (originalW > originalH && originalW > pixelW) {
            simpleSize = originalW / pixelW;
        } else if (originalW < originalH && originalH > pixelH) {
            simpleSize = originalH / pixelH;
        }
        if (simpleSize <= 0) {
            simpleSize = 1;
        }
        return simpleSize;
    }
}



猜你喜欢

转载自blog.csdn.net/qq_36665856/article/details/79613289
今日推荐