/*_x000D_
 * Framework code written for the Multimedia course taught in the first year_x000D_
 * of the UvA Informatica bachelor._x000D_
 *_x000D_
 * Nardi Lam, 2015 (based on code by I.M.J. Kamps, S.J.R. van Schaik, R. de Vries, 2013)_x000D_
 */_x000D_
_x000D_
package comthedudifulmoneymoneymoney.httpsgithub.coincounter;_x000D_
_x000D_
import android.content.Context;_x000D_
import android.graphics.Canvas;_x000D_
import android.graphics.Matrix;_x000D_
import android.util.AttributeSet;_x000D_
import android.util.DisplayMetrics;_x000D_
import android.util.Log;_x000D_
import android.view.View;_x000D_
import android.graphics.Bitmap;_x000D_
_x000D_
_x000D_
/*_x000D_
 * This is a View that displays incoming images._x000D_
 */_x000D_
public class ImageDisplayView extends View implements ImageListener {_x000D_
_x000D_
    CircleDetection CD_cur = new CircleDetection();_x000D_
    CircleDetection CD_done = new CircleDetection();_x000D_
_x000D_
    // Canvas matrix om image te roteren en schalen_x000D_
    Matrix matrix = new Matrix();_x000D_
    // Thread om cirkeldetectie in te draaien_x000D_
    Thread t = null;_x000D_
_x000D_
_x000D_
    /*** Constructors ***/_x000D_
_x000D_
    public ImageDisplayView(Context context) {_x000D_
        super(context);_x000D_
    }_x000D_
_x000D_
    public ImageDisplayView(Context context, AttributeSet attrs) {_x000D_
        super(context, attrs);_x000D_
    }_x000D_
_x000D_
    public ImageDisplayView(Context context, AttributeSet attrs, int defStyle) {_x000D_
        super(context, attrs, defStyle);_x000D_
    }_x000D_
_x000D_
    public int dpToPx(int dp) {_x000D_
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();_x000D_
        int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));_x000D_
        return px;_x000D_
    }_x000D_
_x000D_
    /*** Image drawing ***/_x000D_
_x000D_
    private Bitmap currentImage = null;_x000D_
_x000D_
_x000D_
    @Override_x000D_
    public void onImage(Bitmap argb) {_x000D_
_x000D_
        // Voeg schaling toe aan canvas matrix_x000D_
        matrix.reset();_x000D_
        matrix.postScale(((float) this.getHeight()) / argb.getWidth(), ((float) this.getWidth()) / argb.getHeight());_x000D_
_x000D_
        // Laad nieuwe frame_x000D_
        CD_done.LoadImage(argb);_x000D_
_x000D_
        // Alleen eerste frame (bij opstarten camera)_x000D_
        if (t == null) {_x000D_
            Log.i("Thread", "Threading begonnen");_x000D_
_x000D_
            // Doe eerste berekening in Main thread_x000D_
            CD_done.run();_x000D_
_x000D_
            // Start nieuwe Thread_x000D_
            CD_cur = new CircleDetection(argb);_x000D_
            t = new Thread(CD_cur);_x000D_
            t.start();_x000D_
        }_x000D_
_x000D_
        // Als de Thread klaar is met rekenen_x000D_
        if (!this.t.isAlive()) {_x000D_
_x000D_
            // Einde Thread afhandelen_x000D_
            CD_done = CD_cur;_x000D_
            CD_done.LoadImage(argb);_x000D_
_x000D_
            // Nieuwe Thread beginnen_x000D_
            CD_cur = new CircleDetection(argb);_x000D_
            t = new Thread(CD_cur);_x000D_
            t.start();_x000D_
        }_x000D_
_x000D_
        // Geef frame door_x000D_
        this.currentImage = CD_done.image;_x000D_
        this.invalidate();_x000D_
_x000D_
    }_x000D_
_x000D_
    @Override_x000D_
    protected void onDraw(Canvas canvas) {_x000D_
        super.onDraw(canvas);_x000D_
_x000D_
        /* If there is an image to be drawn: */_x000D_
        if (this.currentImage != null) {_x000D_
_x000D_
            // Teken meest recente cirkels + totaal op frame_x000D_
            CD_done.DrawCircles();_x000D_
            MainActivity.text.setText("Totaal: \u20ac" + String.format("%.2f", CD_done.totaal));_x000D_
_x000D_
            // Pas canvas matrix aan_x000D_
            matrix.postRotate(90);_x000D_
            matrix.postTranslate(canvas.getWidth(), dpToPx(30));_x000D_
_x000D_
            canvas.setMatrix(matrix);_x000D_
            canvas.drawBitmap(CD_done.image, 0, 0, null);_x000D_
        }_x000D_
    }_x000D_
_x000D_
    /*** Source selection ***/_x000D_
    private ImageSource source = null;_x000D_
_x000D_
    public void setImageSource(ImageSource source) {_x000D_
        if (this.source != null) {_x000D_
            this.source.setOnImageListener(null);_x000D_
        }_x000D_
        source.setOnImageListener(this);_x000D_
        this.source = source;_x000D_
    }_x000D_
_x000D_
    public ImageSource getImageSource() {_x000D_
        return this.source;_x000D_
    }_x000D_
_x000D_
}
// Alleen eerste frame (bij opstarten camera)_x000D_
/*_x000D_
 * Framework code written for the Multimedia course taught in the first year_x000D_
 * of the UvA Informatica bachelor._x000D_
 *_x000D_
 * Nardi Lam, 2015 (based on code by I.M.J. Kamps, S.J.R. van Schaik, R. de Vries, 2013)_x000D_
 */_x000D_
_x000D_
package comthedudifulmoneymoneymoney.httpsgithub.coincounter;_x000D_
_x000D_
import android.content.Context;_x000D_
import android.graphics.Canvas;_x000D_
import android.graphics.Matrix;_x000D_
import android.util.AttributeSet;_x000D_
import android.util.DisplayMetrics;_x000D_
import android.util.Log;_x000D_
import android.view.View;_x000D_
import android.graphics.Bitmap;_x000D_
_x000D_
_x000D_
/*_x000D_
 * This is a View that displays incoming images._x000D_
 */_x000D_
public class ImageDisplayView extends View implements ImageListener {_x000D_
_x000D_
    CircleDetection CD_cur = new CircleDetection();_x000D_
    CircleDetection CD_done = new CircleDetection();_x000D_
_x000D_
    // Canvas matrix om image te roteren en schalen_x000D_
    Matrix matrix = new Matrix();_x000D_
    // Thread om cirkeldetectie in te draaien_x000D_
    Thread t = null;_x000D_
_x000D_
_x000D_
    /*** Constructors ***/_x000D_
_x000D_
    public ImageDisplayView(Context context) {_x000D_
        super(context);_x000D_
    }_x000D_
_x000D_
    public ImageDisplayView(Context context, AttributeSet attrs) {_x000D_
        super(context, attrs);_x000D_
    }_x000D_
_x000D_
    public ImageDisplayView(Context context, AttributeSet attrs, int defStyle) {_x000D_
        super(context, attrs, defStyle);_x000D_
    }_x000D_
_x000D_
    public int dpToPx(int dp) {_x000D_
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();_x000D_
        int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));_x000D_
        return px;_x000D_
    }_x000D_
_x000D_
    /*** Image drawing ***/_x000D_
_x000D_
    private Bitmap currentImage = null;_x000D_
_x000D_
_x000D_
    @Override_x000D_
    public void onImage(Bitmap argb) {_x000D_
_x000D_
        // Voeg schaling toe aan canvas matrix_x000D_
        matrix.reset();_x000D_
        matrix.postScale(((float) this.getHeight()) / argb.getWidth(), ((float) this.getWidth()) / argb.getHeight());_x000D_
_x000D_
        // Laad nieuwe frame_x000D_
        CD_done.LoadImage(argb);_x000D_
_x000D_
        // Alleen eerste<SUF>
        if (t == null) {_x000D_
            Log.i("Thread", "Threading begonnen");_x000D_
_x000D_
            // Doe eerste berekening in Main thread_x000D_
            CD_done.run();_x000D_
_x000D_
            // Start nieuwe Thread_x000D_
            CD_cur = new CircleDetection(argb);_x000D_
            t = new Thread(CD_cur);_x000D_
            t.start();_x000D_
        }_x000D_
_x000D_
        // Als de Thread klaar is met rekenen_x000D_
        if (!this.t.isAlive()) {_x000D_
_x000D_
            // Einde Thread afhandelen_x000D_
            CD_done = CD_cur;_x000D_
            CD_done.LoadImage(argb);_x000D_
_x000D_
            // Nieuwe Thread beginnen_x000D_
            CD_cur = new CircleDetection(argb);_x000D_
            t = new Thread(CD_cur);_x000D_
            t.start();_x000D_
        }_x000D_
_x000D_
        // Geef frame door_x000D_
        this.currentImage = CD_done.image;_x000D_
        this.invalidate();_x000D_
_x000D_
    }_x000D_
_x000D_
    @Override_x000D_
    protected void onDraw(Canvas canvas) {_x000D_
        super.onDraw(canvas);_x000D_
_x000D_
        /* If there is an image to be drawn: */_x000D_
        if (this.currentImage != null) {_x000D_
_x000D_
            // Teken meest recente cirkels + totaal op frame_x000D_
            CD_done.DrawCircles();_x000D_
            MainActivity.text.setText("Totaal: \u20ac" + String.format("%.2f", CD_done.totaal));_x000D_
_x000D_
            // Pas canvas matrix aan_x000D_
            matrix.postRotate(90);_x000D_
            matrix.postTranslate(canvas.getWidth(), dpToPx(30));_x000D_
_x000D_
            canvas.setMatrix(matrix);_x000D_
            canvas.drawBitmap(CD_done.image, 0, 0, null);_x000D_
        }_x000D_
    }_x000D_
_x000D_
    /*** Source selection ***/_x000D_
    private ImageSource source = null;_x000D_
_x000D_
    public void setImageSource(ImageSource source) {_x000D_
        if (this.source != null) {_x000D_
            this.source.setOnImageListener(null);_x000D_
        }_x000D_
        source.setOnImageListener(this);_x000D_
        this.source = source;_x000D_
    }_x000D_
_x000D_
    public ImageSource getImageSource() {_x000D_
        return this.source;_x000D_
    }_x000D_
_x000D_
}