国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

圖片的編碼存儲與解碼輸出(基于Android將圖片轉(zhuǎn)Base64編碼后再次解碼會導(dǎo)致圖片失真的解決辦法)

這篇具有很好參考價值的文章主要介紹了圖片的編碼存儲與解碼輸出(基于Android將圖片轉(zhuǎn)Base64編碼后再次解碼會導(dǎo)致圖片失真的解決辦法)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

? ? Android將經(jīng)Bsae64編碼后的圖片存儲到MySQL,并解碼輸出查看(問題:圖片失真)

? ? Android項目下載:

鏈接:https://pan.baidu.com/s/1hvVupGcCf1f41fizF08SNg?pwd=9988?
提取碼:9988

部分源碼:

package com.example.testandroidmysqlphplogin;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


import android.os.Handler;

import android.app.Activity;
import android.content.Intent;


public class MainActivity extends AppCompatActivity {
    protected static final int ERROR = 2;
    protected static final int SUCCESS = 1;
    private EditText et_qq;
    private EditText et_psd;

    Button button,button1;



    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS:
                    Toast.makeText(MainActivity.this,(String)msg.obj, Toast.LENGTH_LONG).show();
                    break;

                case ERROR:
                    Toast.makeText(MainActivity.this,"傳輸失敗", Toast.LENGTH_LONG).show();
                    break;

            }
        };
    };



    private Object StreamTools;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_qq = (EditText) findViewById(R.id.et_qq);
        et_psd = (EditText) findViewById(R.id.et_pwd);




        button1 = findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ImageView image1 = (ImageView) findViewById(R.id.image1);
                EditText et_pwd = (EditText) findViewById(R.id.et_pwd);
                et_pwd.setMovementMethod(new ScrollingMovementMethod());
                et_pwd.setVerticalScrollBarEnabled(true);
                et_pwd.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);


                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qq);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 0, baos);
                byte[] imageBytes = baos.toByteArray();
                String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
                et_pwd.setText(imageString);

                /*decode base64 string to image*/
                imageBytes = Base64.decode(imageString, Base64.DEFAULT);
                Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                image1.setImageBitmap(decodedImage);



            }});




        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ImageView image = (ImageView) findViewById(R.id.image);
                TextView et_pwd = (EditText) findViewById(R.id.et_qq);
                et_qq.setMovementMethod(new ScrollingMovementMethod());
                et_qq.setVerticalScrollBarEnabled(true);
                et_qq.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);


                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ww);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 0, baos);
                byte[] imageBytes = baos.toByteArray();
                String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);


                imageBytes = Base64.decode(imageString, Base64.DEFAULT);
                Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                image.setImageBitmap(decodedImage);
                et_pwd.setText(imageString);




            }});




        }

    public void login(View view){
        final String qq = et_qq.getText().toString();

        Intent i=new Intent(MainActivity.this,MainActivity.class);
        startActivity(i);

        if(TextUtils.isEmpty(qq)){
            Toast.makeText(this, "不能為空", Toast.LENGTH_SHORT).show();
            return;
        }
        new Thread(){
            public void run(){
                try {
                    String path = "http://192.168.228.1/PHP/MySQL/10.php";
                    URL url = new  URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    //區(qū)別2、請求方式post
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0(compatible;MSIE 9.0;Windows NT 6.1;Trident/5.0)");
                    //區(qū)別3、必須指定兩個請求的參數(shù)
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//請求的類型  表單數(shù)據(jù)
                    String data = "username="+qq+"&button=";
                    ;
                    conn.setRequestProperty("Content-Length", data.length()+"");//數(shù)據(jù)的長度
                    //區(qū)別4、記得設(shè)置把數(shù)據(jù)寫給服務(wù)器
                    conn.setDoOutput(true);//設(shè)置向服務(wù)器寫數(shù)據(jù)
                    byte[] bytes = data.getBytes();
                    conn.getOutputStream().write(bytes);//把數(shù)據(jù)以流的方式寫給服務(wù)器
                    int code = conn.getResponseCode();
                    System.out.println(code);
                    if(code == 200){
                        InputStream is = conn.getInputStream();
                        String  result = com.example.testandroidmysqlphplogin.StreamTools.readStream(is);
                        Message mas= Message.obtain();
                        mas.what = SUCCESS;
                        mas.obj = result;
                        handler.sendMessage(mas);

                    }else{
                        Message mas = Message.obtain();
                        mas.what = ERROR;
                        handler.sendMessage(mas);
                    }
                }catch (IOException e) {
                    // TODO Auto-generated catch block
                    Message mas = Message.obtain();
                    mas.what = ERROR;
                    handler.sendMessage(mas);
                }
            }
        }.start();

    }




    public void login1(View view){

        Intent i=new Intent(MainActivity.this,MainActivity.class);
        startActivity(i);

        final String psd = et_psd.getText().toString();

        if(TextUtils.isEmpty(psd)){
            Toast.makeText(this, "不能為空", Toast.LENGTH_SHORT).show();
            return;
        }
        new Thread(){
            public void run(){
                try {
                    String path = "http://192.168.228.1/PHP/MySQL/10.php";
                    URL url = new  URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    //區(qū)別2、請求方式post
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0(compatible;MSIE 9.0;Windows NT 6.1;Trident/5.0)");
                    //區(qū)別3、必須指定兩個請求的參數(shù)
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//請求的類型  表單數(shù)據(jù)
                    String data = "username="+psd+"&button=";
                    ;
                    conn.setRequestProperty("Content-Length", data.length()+"");//數(shù)據(jù)的長度
                    //區(qū)別4、記得設(shè)置把數(shù)據(jù)寫給服務(wù)器
                    conn.setDoOutput(true);//設(shè)置向服務(wù)器寫數(shù)據(jù)
                    byte[] bytes = data.getBytes();
                    conn.getOutputStream().write(bytes);//把數(shù)據(jù)以流的方式寫給服務(wù)器
                    int code = conn.getResponseCode();
                    System.out.println(code);
                    if(code == 200){
                        InputStream is = conn.getInputStream();
                        String  result = com.example.testandroidmysqlphplogin.StreamTools.readStream(is);
                        Message mas= Message.obtain();
                        mas.what = SUCCESS;
                        mas.obj = result;
                        handler.sendMessage(mas);

                    }else{
                        Message mas = Message.obtain();
                        mas.what = ERROR;
                        handler.sendMessage(mas);
                    }
                }catch (IOException e) {
                    // TODO Auto-generated catch block
                    Message mas = Message.obtain();
                    mas.what = ERROR;
                    handler.sendMessage(mas);
                }
            }
        }.start();

    }
}

轉(zhuǎn)Base64編碼部分:

ByteArrayOutputStream baos = new ByteArrayOutputStream()Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ww);
bitmap.compress(Bitmap.CompressFormat.JPEG, 0, baos);
byte[] imageBytes = baos.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);


imageBytes = Base64.decode(imageString, Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
image.setImageBitmap(decodedImage);
et_pwd.setText(imageString);

Android要存儲的目標(biāo)圖片:

base64圖片解碼,android

? ? Android項目運行:

以上Android程序運行后會將圖片的轉(zhuǎn)為Base64編碼進行存儲,以下是圖片轉(zhuǎn)碼后的結(jié)果。

base64圖片解碼,android

? ? ?php將MySQL中編碼解碼后輸出驗證:

下面是進行驗證的php代碼:

<?php

$link=mysqli_connect("localhost","your_username","your_password","your_database");
$sql = "SELECT * FROM user WHERE Id =1";

$res =    mysqli_query($link,$sql);

$data = array();

$imgStr = '';
while ($row = mysqli_fetch_assoc($res)) {

    $imgStr .= $row['ws_username'];

}

$binaryStr = base64_decode($imgStr);
header("Content-type:image/jpg");
echo $binaryStr;

?>

但是解碼后輸出后的照片出現(xiàn)了嚴重失真

base64圖片解碼,android

? ? ?圖片失真原因:

  1. 數(shù)據(jù)量增加:Base64 編碼使用64個字符表示原始二進制數(shù)據(jù),因此會對原始圖片數(shù)據(jù)進行擴展。編碼后的數(shù)據(jù)量通常會比原始二進制數(shù)據(jù)大約 33%。這意味著編碼后的數(shù)據(jù)量更大,可能導(dǎo)致傳輸和存儲方面的效率下降。

  2. 在寫程序時對圖片進行了壓縮,導(dǎo)致很多像素點丟失,致使圖片存儲時就是不完整的。

經(jīng)過資料查詢與多次驗證,找到了解決方法:

將圖片轉(zhuǎn)為二進制數(shù)存儲到MySQL,再通過對二進制碼轉(zhuǎn)為圖片輸出可以完整的輸出圖片,通過php對圖片的存儲過可以確定,Android不失真的存儲圖片,可以將圖片轉(zhuǎn)為二進制圖片進行存儲。

存儲圖片:

base64圖片解碼,android

轉(zhuǎn)碼后存到MySQL:

base64圖片解碼,android

?將二進制碼轉(zhuǎn)圖片輸出:

base64圖片解碼,android

?該理論應(yīng)用于上傳的Android程序中可順利解決Android存圖片輸出失真問題。

php將圖片轉(zhuǎn)二進制碼:

<?php
// 連接到數(shù)據(jù)庫
$conn = mysqli_connect("localhost", "rttt", "128", "my");

$imageData = file_get_contents("F:/編號圖片/手指/001.jpg");// 讀取圖片文件的二進制數(shù)據(jù)

$escapedImageData = mysqli_real_escape_string($conn, $imageData);
//echo $escapedImageData;


// 執(zhí)行INSERT語句將圖片數(shù)據(jù)插入到數(shù)據(jù)庫表中
$sql = "INSERT INTO images (`image_data`) VALUES ('$escapedImageData')";



mysqli_query($conn, $sql);
?>

php將二進制碼轉(zhuǎn)圖片驗證:文章來源地址http://www.zghlxwxcb.cn/news/detail-801597.html

<?php
// 連接到數(shù)據(jù)庫
$conn = mysqli_connect("localhost", "rttt", "128", "my");


// 執(zhí)行查詢語句獲取圖片數(shù)據(jù)
$sql = "SELECT image_data FROM images WHERE id = '1'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);


header("Content-type: image/jpeg");// 設(shè)置HTTP標(biāo)頭為圖像類型


echo $row['image_data'];


mysqli_close($conn);// 關(guān)閉數(shù)據(jù)庫連接
?>

到了這里,關(guān)于圖片的編碼存儲與解碼輸出(基于Android將圖片轉(zhuǎn)Base64編碼后再次解碼會導(dǎo)致圖片失真的解決辦法)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • JS Base64編碼和解碼

    Base64 是一種編碼方式,可以將任意字符(包括二進制字符流)轉(zhuǎn)成可打印字符。JavaScript 定義了兩個與 Base64 相關(guān)的全局方法。 btoa():字符串或二進制值轉(zhuǎn)為 Base64 編碼。 atob():把 Base64 編碼轉(zhuǎn)為原來的字符。 Base64 方法不能夠操作非 ASCII 字符。 示例 要將非 ASCII 碼字符轉(zhuǎn)為

    2024年02月16日
    瀏覽(20)
  • java base64編碼和解碼

    Base64 編碼會將字符串編碼得到一個含有 A-Za-z0-9+/ 的字符串。 base64 編碼和解碼網(wǎng)上有些地方稱為 base64 加密和解密,這是不準(zhǔn)確的。base64 是不能用來加密和解密的,它只是一種編碼解碼方式,不能用于加密和解密,如果你想加密和解密可以選擇如 AES、RSA 等加密算法。 標(biāo)準(zhǔn)的

    2024年02月15日
    瀏覽(32)
  • Python中的Base64編碼和解碼

    Python3中Base64編碼和解碼,使用的是base64模塊中的b64encode 和 b64decode方法,關(guān)于怎么使用,首先查看源碼中的說明: b64encode : Encode the bytes-like object s using Base64 and return a bytes object b64decode : Decode the Base64 encoded bytes-like object or ASCII string s… The result is returned as a bytes object. 要點

    2024年02月13日
    瀏覽(21)
  • 前端Base64 編碼和解碼的使用方法

    使用 Base64 類從 ‘js-base64’ 庫進行 Base64 編碼和解碼 vue: 使用 npm 或 yarn 包管理器來安裝‘js-base64’ 庫 原生: 通過 vue: 導(dǎo)入 Base64 類: 或者: vue: Base64 編碼的使用示例: Base64 解碼的使用示例: 或者,導(dǎo)入 encode 和 decode 函數(shù): Base64 編碼的使用示例: Base64 解碼的使用示例

    2024年02月07日
    瀏覽(26)
  • 【JS】實現(xiàn) Base64 編碼和解碼(及中文亂碼問題)

    JavaScript 定義了兩個與 Base64 相關(guān)的全局方法。 遇到中文編碼需要先做一次 URI 組件編碼或?qū)獯a后的內(nèi)容進行 URI 解碼 示例:Base64 編碼 示例:Base64 解碼 中文亂碼處理方法: 按照 Base64 插件:編解碼: js-base64 ,判斷是否是Base64編碼格式: is-base64 插件使用

    2024年02月15日
    瀏覽(25)
  • php的Url 安全的base64編碼解碼類

    2024年02月04日
    瀏覽(23)
  • Linux系統(tǒng)下使用命令行進行Base64編碼和解碼

    大家知道,編碼是將數(shù)據(jù)轉(zhuǎn)換為有效傳輸或存儲所需格式的過程。相反,解碼是將編碼數(shù)據(jù)轉(zhuǎn)換回其原始格式的過程。今天在這里向大家介紹一下 Base64 編碼和解碼,以便大家理解與學(xué)習(xí)。 Base64 是將二進制數(shù)據(jù)轉(zhuǎn)換為 ASCII 的編碼方式之一。通常,當(dāng)二進制數(shù)據(jù)傳輸?shù)綗o法正

    2024年02月11日
    瀏覽(39)
  • C# 采用3DES-MAC進行簽名 base64解碼與編碼

    C# 采用3DES-MAC進行簽名 base64解碼與編碼

    ** ** 3DES-MAC(Triple Data Encryption Standard Message Authentication Code)是一種消息認證碼(MAC)算法,用于驗證消息的完整性和真實性。3DES-MAC使用了3DES(Triple Data Encryption Standard)加密算法的一部分來生成MAC值。以下是使用C#示例演示如何使用3DES-MAC簽名一個消息: 首先,你需要確保在

    2024年02月09日
    瀏覽(20)
  • 深入了解圖片Base64編碼

    深入了解圖片Base64編碼

    title: 深入了解圖片Base64編碼 date: 2024/4/8 10:03:22 updated: 2024/4/8 10:03:22 tags: Base64編碼 圖片轉(zhuǎn)換 HTTP請求 前端開發(fā) 移動應(yīng)用 性能優(yōu)化 圖片壓縮 Base64編碼是一種將二進制數(shù)據(jù)轉(zhuǎn)換為文本字符串的編碼方式,通過將數(shù)據(jù)轉(zhuǎn)換為一種可打印的ASCII字符集,以便在文本協(xié)議中傳輸。對圖

    2024年04月08日
    瀏覽(51)
  • 【tips】base64編碼怎么反顯出圖片

    【tips】base64編碼怎么反顯出圖片

    實際的數(shù)據(jù)展示是這樣的 然后把以上的文件內(nèi)容放置到html文件中 點擊這個 展示出來是這樣的

    2024年02月05日
    瀏覽(33)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包