? ? 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)圖片:
? ? Android項目運行:
以上Android程序運行后會將圖片的轉(zhuǎn)為Base64編碼進行存儲,以下是圖片轉(zhuǎn)碼后的結(jié)果。
? ? ?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)了嚴重失真
? ? ?圖片失真原因:
-
數(shù)據(jù)量增加:Base64 編碼使用64個字符表示原始二進制數(shù)據(jù),因此會對原始圖片數(shù)據(jù)進行擴展。編碼后的數(shù)據(jù)量通常會比原始二進制數(shù)據(jù)大約 33%。這意味著編碼后的數(shù)據(jù)量更大,可能導(dǎo)致傳輸和存儲方面的效率下降。
-
在寫程序時對圖片進行了壓縮,導(dǎo)致很多像素點丟失,致使圖片存儲時就是不完整的。
經(jīng)過資料查詢與多次驗證,找到了解決方法:
將圖片轉(zhuǎn)為二進制數(shù)存儲到MySQL,再通過對二進制碼轉(zhuǎn)為圖片輸出可以完整的輸出圖片,通過php對圖片的存儲過可以確定,Android不失真的存儲圖片,可以將圖片轉(zhuǎn)為二進制圖片進行存儲。
存儲圖片:
轉(zhuǎn)碼后存到MySQL:
?將二進制碼轉(zhuǎn)圖片輸出:
?該理論應(yīng)用于上傳的Android程序中可順利解決Android存圖片輸出失真問題。
php將圖片轉(zhuǎn)二進制碼:文章來源:http://www.zghlxwxcb.cn/news/detail-801597.html
<?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)!