发布网友
共2个回答
热心网友
1.如何将zxing的Android源码导入工程。
在导入zxing的android源码之前,先去官方下载zxing的源码:http://code.google.com/p/zxing/downloads/list。
我这里下载的是1.6版本的,我试验了几个版本,发现2.0以后的版本实际用起来没有1.6和1.7版本的好用,最后选择了1.6版本。
在导入之前先要对core文件下的源码进行编译,得到核心包core.jar。
编译方法请参照:http://blog.163.com/yimigao@126/blog/static/671560502011611111116747/
然后就可以导入android平台下的例子了,导入方法如下:
1)打开Eclipse,新建android项目:(注意不要直接把android文件夹拷到workspace下导入,那样会无法导入)
2)导入核心包core.jar。
3)修改strings.xml文件。在导入core.jar之后工程还是会有错误:
出现这种错误可能是由于字符错误导致的,只需要把所有的%s 和%f改成 %1s和f 即可。
修改完之后重新清理项目,此时已经没有错误了:
2.代码简化
上面代码中,很多功能我们在自己的项目中都用不到,因此需要对其进行简化,至于如何简化这里就不赘述了,网上有很多教程。下面只给出简化后的结果:
如果只进行二维码识别和二维码生成的话,只需要上面包中的文件。其中CaptureActivity.java是拍照取景的类,camera包下面的类主要与照相机相关的类,decoding和encoding是解码和编码相关的类,view是取景框相关的类。
3.将简化的zxing代码嵌入自己的工程。
在自己的工程中嵌入简化的zxing代码即可实现二维码生成和识别功能。
嵌入方法:
1)将上述简化的代码拖到自己工程目录下;
2)将values文件夹和raw文件夹复制自己工程目录下;
3)建立CaptureActivity.java的布局文件capture.xml。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView
android:id="@+id/preview_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<com.zxing.view.ViewfinderView
android:id="@+id/viewfinder_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Scan Barcode"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_cancel_scan"
android:layout_width="230dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="75dp"
android:text="Cancel"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
3)导入core.jar包
4)修改AndrodMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qrcode"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.VIBRATE" /> <!-- 震动权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" /> <!-- 使用照相机权限 -->
<uses-feature android:name="android.hardware.camera.autofocus" /> <!-- 自动聚焦权限 -->
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 隐藏键盘 --><!-- 全屏 -->
<activity
android:configChanges="orientation|keyboardHidden"
android:name="com.zxing.activity.CaptureActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
</application>
</manifest>
热心网友
转载 项目中需要用到二维码,二维码的码制是PDF417,在做了一番研究之后发现zxing是个不错的开源工具(代码托管在google上面)。为什么选择zxing,由于其他一些工具比如barcode4j(开源,支持读,好像不支持写,最后维护时间在2010年)、barcode(商业版)都不太适合,所以选择了zxing。
zxing并没有提供直接可以使用的jar文件,而是需要自己通过编译源码,生成需要的jar文件。额外说明,zxing利用maven管理自己的代码,并且默认使用了jdk7,代码中也使用了jdk7的一些新特性,基于这些情况,可以适当调整jdk的版本(如果降低jdk的版本,需要改动少量的源码)。
下面直接贴出读写文件的代码:
package test;
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URLDecoder; import java.util.EnumMap; import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap; import com.google.zxing.BufferedImageLuminanceSource; import com.google.zxing.DecodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.Reader; import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.common.HybridBinarizer;
public class ZxingPdfRead {
private static Reader barcodeReader = new MultiFormatReader();
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws Exception {
File testImage = new File(
"E:\\work\\all_workspace\\wp_zxing\\barcode4jTest\\src\\test\\helloworld.png");
BufferedImage image = ImageIO.read(testImage);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
Map<DecodeHintType, Object> hints = new EnumMap<DecodeHintType, Object>(
DecodeHintType.class);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = barcodeReader.decode(bitmap, hints);
String resultText = result.getText();
System.out.println("resultText:" + URLDecoder.decode(resultText, "UTF-8"));
} catch (ReaderException ignored) {
ignored.printStackTrace();
}
}
}
ZxingPdfWrite
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package test;
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URLEncoder;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.pdf417.PDF417Writer;
public class ZxingPdfWrite {
private static final int BLACK = 0xff000000;
private static final int WHITE = 0xFFFFFFFF;
/**
* @param args
* @throws WriterException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub PDF417Writer pdf417Writer = new PDF417Writer();
//注意中文乱码问题 BitMatrix bitMatrix = pdf417Writer.encode(URLEncoder.encode("我是中国人","UTF-8"),
BarcodeFormat.PDF_417, 100, 50);
writeToFile(bitMatrix,"png",new File("E:\\work\\all_workspace\\wp_zxing\\barcode4jTest\\src\\test\\helloworld.png"));
}
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
ImageIO.write(image, format, file);
}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
}
}
return image;
}
}