首页 > 其他分享 >每日打卡

每日打卡

时间:2023-03-18 23:11:07浏览次数:36  
标签:layout parent 每日 import 打卡 id match android

Android stdio连接数据库:

(1)AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chen.myapplication">

<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>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.chen.myapplication.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">

<TextView
android:id="@+id/textView2"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:text="花茶助手"
android:textSize="20dp"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="185dp"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
app:srcCompat="@mipmap/icon" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/button"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="取像" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="43dp"
android:orientation="horizontal"
android:gravity="center_vertical">

<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="50dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"

android:text="价格" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="营养" />

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="品种" />
</RadioGroup>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:background="@drawable/textview_border"
android:layout_height="match_parent"
android:text="识别内容" />
</LinearLayout>

</LinearLayout>
</LinearLayout>

</android.support.constraint.ConstraintLayout>

(3)主函数

package com.example.chen.myapplication;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private ImageView imageView;
private TextView textView;
private Button button;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
//连接数据库
private String url="jdbc:mysql://192.168.65.99:3306/test";
private String user="rootes";
private String password="123456";
PreparedStatement statement=null;
Statement stat=null;
Connection conn=null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageView);
textView=(TextView)findViewById(R.id.textView);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
radioButton1=(RadioButton)findViewById(R.id.radioButton1);
radioButton2=(RadioButton)findViewById(R.id.radioButton2);
radioButton3=(RadioButton)findViewById(R.id.radioButton3);
radioButton1.setOnClickListener(this);
radioButton2.setOnClickListener(this);
radioButton3.setOnClickListener(this);
//为按钮设置一个标记
button.setTag(0);
radioButton1.setTag(1);
radioButton2.setTag(2);
radioButton3.setTag(3);

}
@Override
public void onClick(View v) {
Integer id=(Integer)v.getTag();
if(id==0) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection cn= DriverManager.getConnection("jdbc:mysql://192.168.65.99/test","rootes","123456");
String sql="select name from test";
Statement st=(Statement)cn.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
String mybook=rs.getString("name");
System.out.println(mybook);
Log.i("Mainactivity",mybook);
}
cn.close();
st.close();
rs.close();
System.out.println("连接数据库成功");
} catch (ClassNotFoundException e) {
System.out.println("连接数据库失败");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
//把imageView转换成Bitmap
// Bitmap bitmap=loadBitmapFromView(imageView);
//获取图像对应的RGB值.,并存入数据库中
//GetRGB(bitmap);
// conn=getConnection();
//把bitmap转换成为一个文件
//InputPhoto(bitmap);
//显示新的Bitmap图片
// imageView.setImageBitmap(bitmap);
/// System.out.println("取像");

}else if(id==1){
System.out.println("价格");

}else if(id==2){
System.out.println("营养");
}else if(id==3){
System.out.println("品种");
}

}
///获取图片的RGB值
private void GetRGB(Bitmap bitmap) {
int w=bitmap.getWidth();
int h=bitmap.getHeight();
StringBuffer sql=new StringBuffer();
sql.append("insert into tb_rgb(x,y,r,g,b,rgb)values ");
for(int i=1;i<h;i++){//i表示的位置是y
for(int j=1;j<w;j++){
int argb=bitmap.getPixel(j,i);
int r =( argb>>16)&0xff;
int g =( argb>>8)&0xff;
int b =argb&0xff;
int a =(argb>>24)&0xff;
int rgb =(a<<24)|(r<<16)|(g<<8)|b;
sql.append("("+j+","+i+","+r+","+j+","+b+","+rgb+"),");

// System.out.println("R="+r+",G="+g+",B="+b);
bitmap.setPixel(j,i,Color.RED);
}
}
//插入数据入库
InsertSql(sql.toString());
System.out.println("数据库插入成功");
}

//获取imageView中的图片
private Bitmap loadBitmapFromView(ImageView imageView) {
imageView.setDrawingCacheEnabled(true);
imageView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
imageView.setDrawingCacheBackgroundColor(Color.WHITE);
int w = imageView.getWidth();
int h = imageView.getHeight();
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawColor(Color.WHITE);
//如果不设置canvas的画布为白色,则生成透明
imageView.layout(0, 0, w, h);
imageView.draw(canvas);
return bmp;
}
//连接数据库
public Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection(url, user, password);
System.out.println("数据库连接成功");
} catch (Exception e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
return conn;
}
public void InsertSql(String sql){

String sqls=sql.substring(0,sql.length()-1);
conn=getConnection();//连接数据库
System.out.println("连接数据库成功");
try{
stat=conn.createStatement();
//stat.execute(sqls);
}catch (Exception e){
System.out.println("数据库插入失败");
e.printStackTrace();
}
}
//保存图片文件
private void InputPhoto(Bitmap bitmap) {
FileOutputStream fos;
String imagePath="";
try {
boolean isHasSDCard = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (isHasSDCard) {
File sdRoot = Environment.getExternalStorageDirectory();

File file = new File(sdRoot, Calendar.getInstance().
getTimeInMillis() + ".png");
System.out.println("文件路径"+file.toString());
fos = new FileOutputStream(file);
imagePath = file.getAbsolutePath();
}else {
throw new Exception("创建文件夹失败了");
}
bitmap.compress(Bitmap.CompressFormat.PNG,90,fos);
fos.flush();
fos.close();
}catch (Exception e) {
System.out.println("文件保存失败");
e.printStackTrace();
}
imageView.destroyDrawingCache();
System.out.println("保存文件成功");
}

}

运行结果  :

 

标签:layout,parent,每日,import,打卡,id,match,android
From: https://www.cnblogs.com/yunbianshangdadun/p/17232140.html

相关文章

  • 每日总结 3.18
    今天优化了后台的部分代码,代码行大概40行。主要进行转线的操作。//获取转战点publicStringzhuan(intnum1,intnum2){Connectionconn=Tool.getConnection();......
  • 每日总结--2023/3/17
    课程:计算机网络概率论web学习内容:完成了一个个人介绍的web页面及实验报告继续学习了数据链路层的相关内容完成了每日打卡app记录时间的功能 ......
  • 每日总结--2023/3/18
    周六休息一天学习内容:完成了计算机网络的部分作业完善了每日打卡app的细节修复了北京铁道系统的录入功能bug优化了铁道系统数据库表的......
  • 【每日进步一点点系列】七道精选 数据库 实习面试题
    目录​​前言​​​​1.InnoDB和MyISAM的区别​​​​2.数据库的索引是什么结构,为什么不用哈希表?​​​​3.聚簇索引和非聚簇索引​​​​4.索引怎么实现的B+树,为什么选这......
  • 第五周星期五每日总结
       今日继续完善第一次结对作业的部分,经查找资料,设计出换成查询所需的表结构,将站点名称与站点ID存储到一个表szsubstop表中,将线路号与此线路中所有站点ID存储到szsub......
  • 每日双人总结
    importjava.util.LinkedList;publicclassGraph{publicintnum;//结点总数/**LinkedList<Integer>adj[]*一个存放......
  • 每日总结-23.3.17
    今天写了打卡关于记录和检测连续打卡时长的代码packagecom.example.daka1;importandroidx.appcompat.app.AppCompatActivity;importandroid.content.Intent;impo......
  • 每日总结 3.17
    今天完成了web课程的实验报告一后,继续对查询线路的显示进行了优化处理。页面不会在查询之前显示null,使用foreach进行集合遍历。<%@pagelanguage="java"contentType="......
  • 2023/3/16每日随笔
    今天主要完成了AndroidStudio开发的项目首尾工作,将记事本的添加删除修改的操作进行了完善,加入了闹钟功能,可设置闹铃来提示,同时也进行了打卡功能的完成,但是对于切换角色进......
  • 3.16每日总结
       今天学习了2h,从零开始学安卓(代码写的过于抽象除了界面能运行,报错实在是太多了)。今天学习了SQL语句的基本语法和sqlite的基本内容SELECT -从数据库中提取数据......