Android
 Computer >> コンピューター >  >> プログラミング >> Android

AndroidアプリでBase64文字列をビットマップ画像に変換するにはどうすればよいですか?


この例は、AndroidでIを実行する方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ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 −次のコードをsrc / MainActivity.java

に追加します
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;
import androidx.appcompat.app.AppCompatActivity;
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);
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
      byte[] imageBytes = byteArrayOutputStream.toByteArray();
      String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
      imageBytes = Base64.decode(imageString, Base64.DEFAULT);
      Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
      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>
アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[実行]をクリックします ツールバーのAndroidアプリでBase64文字列をビットマップ画像に変換するにはどうすればよいですか? アイコン。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-

AndroidアプリでBase64文字列をビットマップ画像に変換するにはどうすればよいですか?


  1. WordPressサイトをAndroidアプリに変換する方法

    モバイルデバイスの普及は、ますます多くの人々がより小さな画面を使用してインターネットをサーフィンしていることを意味します。 WordPressを使用している場合、サイトがすべての視聴者に見栄えがするようにする方法の1つは、レスポンシブテーマを選択することです。レスポンシブテーマは、ユーザーの画面のサイズに基づいて自動的にサイズを変更します。さらに、モバイルデバイスは低速のネットワークで動作します。 すばやく読み込まれるウェブサイトが重要です。サイトを合理化することに加えて、ホスティングプロバイダーがボトルネックとして機能していないことを確認する必要があります(ホストがボトルネックになっている

  2. AndroidでOCRを使用して画像をテキストに変換する方法

    OCR(光学式文字認識)技術は、近年かなり進歩しています。画像からテキストをより正確にスキャンできるようになりました。モバイルデバイスで画像をテキストに変換できたらいいのではないでしょうか。これにより、画像をコンピュータにアップロードしてオンラインで変換する手間を省くことができます。この記事では、Androidデバイスを使用して画像をテキストに変換する方法を紹介します。 Androidで画像をテキストに変換する Playストアには多くのOCRベースのアプリがありますが、すべてが希望どおりに正確に機能するわけではありません。ただし、十分なテストを行った結果、正常に機能するアプリがいくつか見つか