由于我之前完全没有接触过QT,所以编写出来的界面很丑,QT在VS里自带一个界面设计师,类似于下图,编写个界面还是很方便的,对相关的控制直接写响应函数就可以了。
添加菜单和标签栏
实现一个从本地文件选取一张要识别图片的效果
对扑克牌图片进行识别,调用到前两章写到的函数
对识别结果进行显示
最后的效果如下
多张有些误差,将红桃10识别成了红桃9,梅花7识别成了黑桃7,其余正确,因为模板数量较小的问题,所以这个误差我还是可以接受的。
到这就完成了整个小程序的开发。现将程序的源码链接放在下面
//download.csdn.net/download/fyueqiao/12072016
package cardimport java.util.Hashtable
public class Card {
/**
* 0:方块 3:黑桃 4:王
*/
public static final int DIAMODE=0
/**1:梅花*/
public static final int CLUB=1
/**2:红桃*/
public static final int HEART=2
/**3:黑桃*/
public static final int SPADE=3
/**4:王*/
static final int WANG=4
/**0:方块 3:黑桃 4:王*/
private static String[] SUIT_NAMES=new String[]{"方块","梅花","红桃","黑桃","王"}
public static final int THREE=0
public static final int FOUR=1
public static final int FIVE=2
public static final int SIX=3
public static final int SEVEN=4
public static final int EIGHT=5
public static final int NINE=6
public static final int TEN=7
public static final int JACK=8
public static final int QUEEN=9
public static final int KING=10
public static final int ACE=11
public static final int DUCK=12
public static final int BALCK=13
public static final int COLOR=14
public static String[] RANK_NAMES=new String[]{"3","4","5","6","7"
,"8","9","10","J","Q","K","A","2","小","大"}
private int suit,rank
// private static class Entry{
// private String name
// private int value
// public Entry(String name,int value) {
// this.name=name
// this.value=value
// }
// public int getValue() {
// return value
// }
// public void setValue(int value) {
// this.value = value
// }
// @SuppressWarnings("unused")
// public String getName() {
// return name
// }
// @SuppressWarnings("unused")
// public void setName(String name) {
// this.name = name
// }
// }
//public static Entry[] table = new Entry[120]
static Hashtable<String, Integer>table = new Hashtable<String,Integer>()
static{
for (int i = 0i <SUIT_NAMES.lengthi++) {
// Entry entry = new Entry(SUIT_NAMES[i],i)
// table[SUIT_NAMES[i].hashCode()%120]=entry
table.put(SUIT_NAMES[i], i)
}
for (int i = 0i <RANK_NAMES.lengthi++) {
//Entry entry = new Entry(RANK_NAMES[i],i)
//table[RANK_NAMES[i].hashCode()%120]=entry
table.put(RANK_NAMES[i], i)
}
}
public int getSuit() {
return suit
}
public void setSuit(int suit) {
this.suit = suit
}
public int getRank() {
return rank
}
public void setRank(int rank) {
this.rank = rank
}
public Card(int suit,int rank) {
setSuit(suit)
setRank(rank)
}
public Card(String strName){
String suitName
String rankName
if(strName.endsWith("王")){
suitName=strName.substring(1)
rankName=strName.substring(0,1)
}else{
suitName=strName.substring(0,2)
rankName=strName.substring(2)
}
setSuit(findSuit(suitName))
setRank(findRank(rankName))
}
private int findSuit(String suitName) {
//return table[suitName.hashCode()%120].getValue()
return table.get(suitName)
}
private int findRank(String rankName) {
return table.get(rankName)
}
@Override
public String toString() {
if(suit==Card.WANG)
return RANK_NAMES[rank]+SUIT_NAMES[suit]
return SUIT_NAMES[suit]+RANK_NAMES[rank]
}
@Override
public boolean equals(Object obj) {
if(obj==null)
return false
if(obj==this)
return true
if(obj instanceof Card){
Card other=(Card)obj
return other.rank==rank &&other.suit==suit
}
return false
}
@Override
public int hashCode() {
return rank*10+suit
}
}
/*************************************************************************************/
package card
import java.util.ArrayList
import java.util.Collections
import java.util.Iterator
import java.util.List
public class CardDemo {
/**
* @param args
*/
public static void main(String[] args) {
// Card c=new Card(Card.HEART,Card.THREE)
// System.out.println(c.toString())
// c=new Card("黑桃A")
// System.out.println(c.toString())
List<Card>cards = new ArrayList<Card>()
for (int i = 0i <= Card.DUCKi++) {
for (int j = 0j <= Card.SPADEj++) {
cards.add(new Card(j, i))
}
}
cards.add(new Card("小王"))
cards.add(new Card("大王"))
// Random r = new Random()
// for (int i =1i <cards.size()i++) {
// int j=r.nextInt(i)
// Card temp=cards.get(i)
// cards.set(i, cards.get(j))
// cards.set(j, temp)
// }
Collections.shuffle(cards)
System.out.println(cards.toString())
Player[] players = new Player[3]
players[0] = new Player(0, "萧炎")
players[1] = new Player(1, "罗峰")
players[2] = new Player(2, "小胖")
// for (int i = 0i <cards.size()i++) {
// players[i%players.length].add(cards.get(i))
// }
// for (int i = 0i <players.lengthi++) {
// System.out.println(players[i].toString())
// }
Iterator<Card>ite = cards.iterator()
int c=0
while(ite.hasNext()){
players[(c++)%players.length].add(ite.next())
ite.remove()
}
for (int i = 0i <players.lengthi++) {
System.out.println(players[i])
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)