jp.maju.wifiserver.camera.QRReaderActivity.java Source code

Java tutorial

Introduction

Here is the source code for jp.maju.wifiserver.camera.QRReaderActivity.java

Source

package jp.maju.wifiserver.camera;

import jp.maju.wifidetecter.utils.Logger;
import jp.maju.wifiserver.GateActivity;
import jp.maju.wifiserver.R;
import jp.maju.wifiserver.SocketInfo;
import jp.maju.wifiserver.R.id;
import jp.maju.wifiserver.R.layout;
import jp.maju.wifiserver.R.style;
import jp.maju.wifiserver.client.ClientActivity;
import jp.maju.wifiserver.util.CommonUtil;
import jp.maju.wifiserver.util.PreferenceUtil;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.os.Bundle;
import android.support.v4.view.WindowCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
import android.widget.Toast;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;

/*
 * Copyright {2014} {Matsuda Jumpei}
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
public class QRReaderActivity extends ActionBarActivity implements OnClickListener {
    private static final String TAG = QRReaderActivity.class.getSimpleName();

    private CameraSurfaceView mCameraSurfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_AppCompat);

        requestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY);

        setContentView(R.layout.activity_qr_reader);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            startActivity(new Intent(this, GateActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        mCameraSurfaceView = ((CameraSurfaceView) findViewById(R.id.surface_camera))
                .initialize(((SeekBar) findViewById(R.id.seek_zoom)));

        mCameraSurfaceView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.surface_camera:
            mCameraSurfaceView.oneShot(new PreviewCallback() {

                @Override
                public void onPreviewFrame(byte[] data, Camera camera) {
                    int previewWidth = camera.getParameters().getPreviewSize().width;
                    int previewHeight = camera.getParameters().getPreviewSize().height;

                    PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, previewWidth,
                            previewHeight, 0, 0, previewWidth, previewHeight, false);
                    BinaryBitmap biBitmap = new BinaryBitmap(new HybridBinarizer(source));

                    Result result = null;
                    try {
                        result = new MultiFormatReader().decode(biBitmap);
                        String text = result.getText();
                        String[] params = text.split(":");

                        if (CommonUtil.getCurrentSSID(getApplication()).equals(params[0])) {
                            SocketInfo si = new SocketInfo(params[0]);
                            si.setHost(params[1]);
                            si.setPort(Integer.parseInt(params[2]));
                            si.setKind(params[3]);

                            Intent intent = new Intent(QRReaderActivity.this, ClientActivity.class)
                                    .putExtra(ClientActivity.KEY_SOCKET_INFO, si);
                            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

                            PreferenceUtil.save(getApplication(), si);
                            PreferenceUtil.setDefaultSide(getApplication(), 2);
                            startActivity(intent);

                            finish();
                        }
                        Logger.d(TAG, "Result decoded:" + text);
                    } catch (Exception e) {
                        Logger.e(TAG, e);

                        Toast.makeText(getApplicationContext(), "Failed to read", Toast.LENGTH_SHORT).show();
                    }
                }
            }, 3);
            break;
        }
    }

}