Android image data format conversion url image address to base64, url to Bitmap, Bitmap to base64, base64 to Bitmap, ImageView loading Bitmap, rotating image angle

Android common image processing:

1. URL image address to base64

     If you want to convert the image address to base64 format, you need to convert it to Bitmap first, and then convert Bitmap to base64, use the following 2 and 3

2.URL to Bitmap

     Since the image is obtained by the address, the thread must be opened for processing during the conversion process. The code is as follows:

public void urlToBitMap(final String url){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = null;
                URL imageurl = null;

                try {
                    imageurl = new URL(url);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                try {
                    HttpURLConnection conn = (HttpURLConnection)imageurl.openConnection();
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    bitmap = BitmapFactory.decodeStream(is);
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
}

 

3.Bitmap to base64

public static String bitmapToBase64(Bitmap bitmap){
        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

 

4.base64 to Bitmap

private Bitmap base64ToBitmap(String base64Img){
        byte[] decodedString = Base64.decode(base64Img.split(",")[1], Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        return decodedByte;
}

5.ImageView loads Bitmap images

imageView.setBackground(new BitmapDrawable(bitmapAll));

6. Rotate the Bitmap format picture angle

    /**
     * 旋转图片
     * @param angle  旋转的角度 例如90,例如180
     * @param bitmap 传入的图片bitmap
     * @return Bitmap  返回旋转后的图片bitmap
     */
    public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();

        matrix.postRotate(angle);
        System.out.println("angle2=" + angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),         bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }

 

Guess you like

Origin blog.csdn.net/qq_37980878/article/details/110577219