venerdì 3 aprile 2015

CE Fake Reveal: sovrapporre un'immagine CE scalabile sullo sfondo della fotocamera.

All'avvio dell'applicazione si aprirà uno sfondo live della fotocamera e verrà sovrapposta una scritta CE con le misure conforme al marchio CE. Questa immagine è scalabile e con un minimo di alpha, mantenendo le proporzioni, in modo da confrontarla con l'immagine CE sull'oggetto da verificare.
Ricordarsi di aggiungere su manifest le seguenti righe per abilitare l'uso della fotocamera:

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />


CE_Fake_Reveal.java :

package com.example.marco.cefakereveal;


import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.graphics.Matrix;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
import android.widget.TextView;


public class CE_Fake_Reveal extends Activity implements SurfaceHolder.Callback{

    ImageView img;
    private Matrix matrix = new Matrix();
    private float scale = 0.2f;
    private ScaleGestureDetector SGD;
    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_ce__fake__reveal);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        img = (ImageView)findViewById(R.id.imageView1);
        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        SGD = new ScaleGestureDetector(this,new ScaleListener());

        scale = 1.0f;

        img.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
        {
        updateScale();

        }
        });




     }



    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        SGD.onTouchEvent(ev);
        return true;
    }

    void updateScale()
    {
        final float imageWidth = (float)img.getDrawable().getIntrinsicWidth();
        final float imageHeight = (float)img.getDrawable().getIntrinsicHeight();
        float frameHeight = img.getHeight();
        float frameWidth = img.getWidth();

        matrix.reset();
        matrix.postTranslate(-imageWidth / 2.0f, -imageHeight / 2.0f);
        matrix.postScale(scale, scale);
        matrix.postTranslate(frameWidth / 2.0f, frameHeight / 2.0f );
        img.setImageMatrix(matrix);

    }





    private class ScaleListener extends ScaleGestureDetector.
            SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scale *= detector.getScaleFactor();
            scale = Math.max(0.03f, Math.min(scale, 0.5f));
            updateScale();
            return true;

        }
    }






/* *************************************************/
/* Ora inizia il codice che riguarda la fotocamera */
/* *************************************************/





    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
// TODO Auto-generated method stub
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                Camera.Parameters parameters = camera.getParameters();
                parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                camera.setParameters(parameters);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }




}

activity_ce_fake_reveal.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">

    <SurfaceView
        android:id="@+id/camerapreview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <ImageView
        android:id="@+id/imageView1"

        android:scaleType="matrix"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:src="@drawable/ce"
        android:layout_centerInParent="true"
        android:alpha="0.4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Confrontare la scritta CE per verificarne la validità"
        android:id="@+id/textView"
        android:textStyle="bold"
        android:textColor="#fffdff12"
        android:layout_alignParentTop="true"
        android:textAlignment="center"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

1 commento: