Android使用 BASE64Encoder实现图片的Base64编码转换(可用来存储到数据库中)学习笔记(一)

Android使用 BASE64Encoder实现图片的Base64编码转换(可用来存储到数据库中)学习笔记(一)

关于

将图片保存为base64码的好处就是可以存储到数据库中,有人说图片的路径不也可以吗,但是这样当本地图片删掉就没了,而且图片转64编码的实现方法也很简单。

效果图

在这里插入图片描述

实现第一步,修改activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorPrimaryDark">
        <LinearLayout
            android:id="@+id/layout_back"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                //这里的都可以注释掉,与本篇无关
                android:background="@drawable/back" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="2dp"
            android:gravity="center"
            android:orientation="horizontal">
        </LinearLayout>
    </android.support.v7.widget.Toolbar>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_weight="1"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/image_base64"
            android:layout_width="150dp"
            android:layout_height="150dp"
            />
        <EditText
            android:id="@+id/edit_base664"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <Button
        android:id="@+id/btn_photo"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="25dp"
         //自己设置颜色
        android:background="@drawable/normal_btn_pushstyle"
        android:text="拍照"
        android:textColor="#fff"
        android:textSize="16sp" />
</LinearLayout>

第二步,修改BASE64Encoder.java

package com.example.imagebase64;

import java.io.IOException;
import java.io.OutputStream;

public class BASE64Encoder extends CharacterEncoder {
   
   
    protected int bytesPerAtom() {
   
   
        return 3;
    }

    protected int bytesPerLine() {
   
   
        return 57;
    }

    private static final char[] pem_array =
            {
   
   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
                    'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
                    'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                    'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
### MSP微控制器中四相编码器QEI的工作原理 四相编码器接口(Quadrature Encoder Interface, QEI)用于处理来自增量式旋转编码器的信号。该接口能够解析正交编码脉冲并将其转换成位置数据,从而实现精确的位置测量。 在MSP微控制器上,当配置为四相模式时,QEI模块会监控两个通道A和B上的输入波形。这两个通道提供相互之间有90度相位差的方波信号。通过监测这些信号的变化情况,可以确定电机轴或其他机械部件转动的方向以及移动的距离[^1]。 如果发生任何异常事件,比如丢失步进或非法状态转移,则触发QEIERR中断来报告这些问题,在这种情况下,计数器不会更新其数值以保持当前记录的状态不变。 ### 应用实例:基于MSP432P401R控制直流电机的速度反馈系统 为了展示如何利用QEI功能构建实际项目,下面给出个简单的例子——使用TI公司的MSP432P401R开发板连接到带有内置霍尔效应传感器阵列的直流无刷马达上来创建速度控制系统: #### 硬件设置 - 连接编码器输出线至MCU相应引脚(P7.6/P7.7),确保电源供电正常; #### 软件编程要点 初始化QEIModule函数设定工作参数如下: ```c void QEIModule_init(void){ // 配置GPIO端口作为QEI输入 GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P7, GPIO_PIN6 | GPIO_PIN7); // 设置QEI模块分频系数及时钟源 QEIVelocityConfigure(QEIA_BASE, QEIClockSource_SystemClock, 8); // 启用错误检测机制 QEIEnableInterrupts(QEIA_BASE,QEI_INT_ERROR); } ``` 编写定时器ISR读取当前位置信息并与目标值比较调整PWM占空比达到闭环调节目的: ```c // 定义全局变量存储期望角度 volatile uint32_t desired_position; // 定时器周期性调用此回调函数 void Timer_A_periodic_callback(){ int current_pos=QEIPositionGet(QEIA_BASE); if(current_pos<desired_position){ PWM_duty_cycle_increase(); }else{ PWM_duty_cycle_decrease(); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪の星空朝酱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值