Android画像データ形式の変換URL画像アドレスからbase64、urlからBitmap、Bitmapからbase64、base64からBitmap、ImageViewの読み込みBitmap、回転画像角度

Androidの一般的な画像処理:

1.base64へのURL画像アドレス

     イメージアドレスをbase64形式に変換する場合は、最初にビットマップに変換してから、ビットマップをbase64に変換する必要があります。次の2と3を使用してください。

2.ビットマップへのURL

     イメージはアドレスによって取得されるため、変換プロセス中に処理するためにスレッドを開く必要があります。コードは次のとおりです。

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.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からビットマップ

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はビットマップ画像をロードします

imageView.setBackground(new BitmapDrawable(bitmapAll));

6.ビットマップ形式の画像の角度を回転させます

    /**
     * 旋转图片
     * @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;
    }

 

おすすめ

転載: blog.csdn.net/qq_37980878/article/details/110577219