Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android ऐप में Base64 स्ट्रिंग को Bitmap इमेज में कैसे बदलें?

Base64 स्ट्रिंग को Bitmap में डिकोड करने का तरीका

यह उदाहरण दर्शाता है कि Base64 एन्कोडेड स्ट्रिंग को वापस Bitmap इमेज में कैसे बदला जाए और ImageView में प्रदर्शित किया जाए।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनें।

चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Base64 string into a BitMap image in Android App"
        android:textSize="16sp"
        android:textStyle="bold|italic" />

</LinearLayout>

चरण 3: MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;

    @SuppressLint("WrongThread")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);

        // 1. ड्रॉएबल को Bitmap में बदलें
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

        // 2. Bitmap को Base64 स्ट्रिंग में एन्कोड करें
        byte[] imageBytes = byteArrayOutputStream.toByteArray();
        String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);

        // 3. Base64 स्ट्रिंग को वापस Bitmap में डिकोड करें
        byte[] decodedBytes = Base64.decode(imageString, Base64.DEFAULT);
        Bitmap decodedImage = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);

        // 4. ImageView में सेट करें
        imageView.setImageBitmap(decodedImage);
    }
}

चरण 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

व्यावहारिक उपयोग

  • सर्वर से Base64 स्ट्रिंग प्राप्त होने पर इमेज दिखाने के लिए।
  • डेटाबेस/SharedPreferences में इमेज स्टोर करने के लिए।
  • JSON API के माध्यम से इमेज ट्रांसफर के लिए।

नोट: बड़ी इमेज के लिए Base64 अक्षम है (साइज ~33% बढ़ जाता है)। प्रोडक्शन में URL या फाइल ट्रांसफर बेहतर है।

  1. Android में Glide लाइब्रेरी से इमेज को Bitmap में कैसे डाउनलोड करें? Android में Glide लाइब्रेरी से इमेज को Bitmap में कैसे डाउनलोड करें?

    परिचय Glide (बम्पटेक द्वारा) एक शक्तिशाली इमेज लोडिंग लाइब्रेरी है जो इमेज को Bitmap ऑब्जेक्ट के रूप में डाउनलोड करने की अनुमति देती है। यह तब उपयोगी है जब आपको इमेज पर प्रोसेसिंग करनी हो या उसे ImageView के बाहर उपयोग करना हो। आवश्यक चरण चरण 1: नया प्रोजेक्ट बनाएं और डिपेंडेंसी जोड़ें Android Stu

  1. WordPress साइट को Android ऐप में कैसे बदलें WordPress साइट को Android ऐप में कैसे बदलें

    मोबाइल उपकरणों की सर्वव्यापकता का मतलब है कि अधिक से अधिक लोग छोटी स्क्रीन का उपयोग करके इंटरनेट सर्फ कर रहे हैं। यदि आप WordPress का उपयोग कर रहे हैं, तो यह सुनिश्चित करने के तरीकों में से एक है कि आपकी साइट सभी दर्शकों को अच्छी लगे, एक रेस्पॉन्सिव थीम चुनना है। रेस्पॉन्सिव थीम उपयोगकर्ता की स्क्री

  1. एंड्रॉइड पर OCR का उपयोग करके इमेज को टेक्स्ट में कैसे बदलें एंड्रॉइड पर OCR का उपयोग करके इमेज को टेक्स्ट में कैसे बदलें

    OCR (ऑप्टिकल कैरेक्टर रिकग्निशन) तकनीक में हाल के वर्षों में काफी सुधार हुआ है। हम अब इमेज से टेक्स्ट को अधिक सटीकता से स्कैन कर सकते हैं। क्या यह अच्छा नहीं होगा यदि हम अपने मोबाइल डिवाइस पर ही इमेज को टेक्स्ट में बदल सकें? यह आपको इमेज को अपने कंप्यूटर पर अपलोड करने और इसे ऑनलाइन बदलने की परेशानी