二维码扫描

xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/capture_containter"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <SurfaceView
        android:id="@+id/capture_preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <ImageView
        android:id="@+id/top_mask"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/scan_mask"
        android:contentDescription="@string/app_name" />
    <RelativeLayout
        android:id="@+id/capture_crop_layout"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_below="@id/top_mask"
        android:layout_centerHorizontal="true"
        android:background="@drawable/capture"
        android:contentDescription="@string/app_name" >
        <ImageView
            android:id="@+id/capture_scan_line"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_margin="5dp"
            android:background="@drawable/scan_line"
            android:contentDescription="@string/app_name" />
    </RelativeLayout>
    <ImageView
        android:id="@+id/bottom_mask"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/capture_crop_layout"
        android:background="@drawable/scan_mask"
        android:contentDescription="@string/app_name" />
    <ImageView
        android:id="@+id/left_mask"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_mask"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/top_mask"
        android:layout_toLeftOf="@id/capture_crop_layout"
        android:background="@drawable/scan_mask"
        android:contentDescription="@string/app_name" />
    <ImageView
        android:id="@+id/right_mask"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_mask"
        android:layout_alignParentRight="true"
        android:layout_below="@id/top_mask"
        android:layout_toRightOf="@id/capture_crop_layout"
        android:background="@drawable/scan_mask"
        android:contentDescription="@string/app_name" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/capture_crop_layout"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="自动扫描中......."
        android:textColor="@android:color/white"
        android:textSize="15sp" />
    <Button
        android:id="@+id/quxiao"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:text="取  消"
        android:textColor="#000000"
        android:textSize="14sp" >
    </Button>
</RelativeLayout>

代码

package com.itheima.frameanimation;


/**
 * zpp
 *[email protected] 2016.1.10
 */
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;


import com.sunlighttech.ihotel.qr.InitElement;
import com.sunlighttech.ihotel.qr.util.OkCallbacks;


public class MainActivity extends Activity implements OkCallbacks {
// capture_containter显示整个画面的Layout
private RelativeLayout mContainer = null;
// 二维码区域
private RelativeLayout mCropLayout = null;
private Button quxiaoButton;
private InitElement initE;
private SurfaceView surfaceView;
private AssetFileDescriptor descriptor;
@SuppressWarnings("unused")
private Intent intent;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
initaction();
}


private void initview() {
mContainer = (RelativeLayout) findViewById(R.id.capture_containter);
mCropLayout = (RelativeLayout) findViewById(R.id.capture_crop_layout);
quxiaoButton = (Button) findViewById(R.id.quxiao);
ImageView mQrLineView = (ImageView) findViewById(R.id.capture_scan_line);
/**
* 设置平移动画
*/
TranslateAnimation mAnimation = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE,
0f, TranslateAnimation.RELATIVE_TO_PARENT, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.9f);
// -1表示无限循环
mAnimation.setRepeatCount(-1);
mAnimation.setDuration(3000);
mAnimation.setRepeatMode(Animation.REVERSE);


mAnimation.setInterpolator(new LinearInterpolator());
// 设置动画
mQrLineView.setAnimation(mAnimation);
quxiaoButton.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View arg0) {
finish();
}
});
}


private void initaction() {
surfaceView = (SurfaceView) findViewById(R.id.capture_preview);
descriptor = getResources().openRawResourceFd(R.raw.beep);
initE = new InitElement(this, mContainer, mCropLayout, descriptor);
initE.initSurfaceHolder(surfaceView);
}


@Override
protected void onResume() {
super.onResume();
initaction();
}


@Override
protected void onPause() {
super.onPause();
initE.actPause();
}


@Override
protected void onDestroy() {
super.onDestroy();
initE.actDestory();
}


/**
* 扫描返回的结果
*/
@Override
public void isOk(String date) {
/**
* 根据自己需求来写 这里就先写个跳转网页的
*/
// 跳转指定的网址
try {
// 通过回调得到达特数据
Toast.makeText(MainActivity.this, date, Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri uri = Uri.parse(date);
intent.setData(uri);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}

下面有demo下载地址:http://download.csdn.net/detail/qq_30442585/9482887


你可能感兴趣的:(二维码,扫描)