Skip to content

Commit

Permalink
Added camera switch.
Browse files Browse the repository at this point in the history
  • Loading branch information
filipkofron committed Apr 25, 2014
1 parent 04f4cb7 commit 5f07abf
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
Expand All @@ -18,6 +19,9 @@
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.IOException;
Expand All @@ -27,6 +31,8 @@
import cz.kofron.foodinventory.client.barcode.DecodeThread;
import cz.kofron.foodinventory.client.barcode.Decoder;
import cz.kofron.foodinventory.client.barcode.ResultCallback;
import cz.kofron.foodinventory.client.preference.Preferences;
import cz.kofron.foodinventory.client.view.SuspendableSurfaceView;

/**
* Created by kofee on 3/9/14.
Expand All @@ -37,7 +43,8 @@ public abstract class AbstractScanFragment extends Fragment implements SurfaceHo
private final static int RESULT_PERIOD_MS = 1500;
private final static int MAX_AREA = 1000 * 1000;
private boolean lockCameraUse = false;
private SurfaceView surfaceView;
private SuspendableSurfaceView surfaceView;
private ViewGroup cont;
private Camera camera;
private boolean previewRunning = false;
private DecodeThread decodeThread;
Expand All @@ -56,16 +63,42 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
{
View view = inflater.inflate(R.layout.scan_fragment, null);

surfaceView = (SurfaceView) view.findViewById(R.id.surface_view);
surfaceView.setKeepScreenOn(true);
final SurfaceHolder surfaceHolder = surfaceView.getHolder();
cont = (ViewGroup) view.findViewById(R.id.container);

View content = view.findViewById(R.id.content);

surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

final EditText editText = (EditText) view.findViewById(R.id.gtin);
initializeContent(content);

return view;
}

Button button = (Button) view.findViewById(R.id.search_button);
private void initializeContent(View content)
{
surfaceView = (SuspendableSurfaceView) content.findViewById(R.id.surface_view);
initializeSurfaceView();

final ImageButton cameraSwapButton = (ImageButton) content.findViewById(R.id.camera_swap_button);
cameraSwapButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
boolean prev = Preferences.getPreferences(AbstractScanFragment.this.getActivity()).getBoolean("camera_back", true);
SharedPreferences.Editor editor = Preferences.getPreferences(AbstractScanFragment.this.getActivity()).edit();
editor.putBoolean("camera_back", !prev);
editor.commit();


stopCameraScan();
startCameraScan();
recreateView();
}
});

final EditText editText = (EditText) content.findViewById(R.id.gtin);

Button button = (Button) content.findViewById(R.id.search_button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
Expand All @@ -78,7 +111,36 @@ public void onClick(View view)
}
}
});
return view;
}

private void recreateView()
{
LayoutInflater li = LayoutInflater.from(getActivity());
ViewGroup viewGroup = (ViewGroup) li.inflate(R.layout.scan_fragment, null);

View content = viewGroup.findViewById(R.id.content);
viewGroup.removeView(content);

cont.removeAllViews();

initializeContent(content);

RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);

cont.addView(content, layoutParams);
}

private void initializeSurfaceView()
{
surfaceView.setKeepScreenOn(true);
final SurfaceHolder surfaceHolder = surfaceView.getHolder();

surfaceHolder.removeCallback(this);

surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void onManualSearch(String text)
Expand Down Expand Up @@ -168,20 +230,25 @@ private void turnOnCamera()
{
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++)
boolean back = Preferences.getPreferences(AbstractScanFragment.this.getActivity()).getBoolean("camera_back", true);

if(back)
{
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
for (int camIdx = 0; camIdx < cameraCount; camIdx++)
{
try
{
camera = Camera.open(camIdx);
break;
}
catch (RuntimeException e)
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
{
camera = null;
e.printStackTrace();
try
{
camera = Camera.open(camIdx);
break;
}
catch (RuntimeException e)
{
camera = null;
e.printStackTrace();
}
}
}
}
Expand Down Expand Up @@ -368,6 +435,34 @@ public synchronized void onPreviewFrame(byte[] bytes, Camera camera)
public void onResume()
{
super.onResume();
startCameraScan();
}

public void setResult(final String text)
{
Activity activity = getActivity();
if(activity != null)
{
getActivity().runOnUiThread(new Runnable()
{
@Override
public void run()
{
onGtin(text);
}
});
}
}

@Override
public void onPause()
{
super.onPause();
stopCameraScan();
}

public void startCameraScan()
{
turnOnCamera();
decodeThread = new DecodeThread(camera, new Decoder(new ResultCallback()
{
Expand Down Expand Up @@ -398,26 +493,8 @@ public void run()
decodeThread.start();
}

public void setResult(final String text)
{
Activity activity = getActivity();
if(activity != null)
{
getActivity().runOnUiThread(new Runnable()
{
@Override
public void run()
{
onGtin(text);
}
});
}
}

@Override
public void onPause()
public void stopCameraScan()
{
super.onPause();
previewRunning = false;
DecodeThread dt = decodeThread;
decodeThread = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected int sizeOf(String key, Bitmap value)
public LoadImageTask(LoadImageParam param)
{
this.param = param;
if(!Preferences.getPreferences(param.context).getBoolean("download_images", true))
if (!Preferences.getPreferences(param.context).getBoolean("download_images", true))
{
param.id = "0";
}
Expand All @@ -74,37 +74,52 @@ protected Void doInBackground(Object... objects)
bitmap = getCache().get(param.id);
}

if(bitmap == null)

if (bitmap == null)
{
synchronized (lock)
{
if (getImageCache(param.context).containsKey(param.id))
try
{
if (getImageCache(param.context).containsKey(param.id))
{
bitmap = imageCache.getBitmap(param.id);
}
}
catch(Exception e)
{
bitmap = imageCache.getBitmap(param.id);
e.printStackTrace();
}
}
}
if(bitmap == null)
if (bitmap == null)
{
if(Preferences.getPreferences(param.context).getBoolean("download_images", true) || param.id.equals("0"))
if (Preferences.getPreferences(param.context).getBoolean("download_images", true) || param.id.equals("0"))
{
bitmap = Download.downloadImage("http://" + Connector.SERVER_ADDR + "/img/" + param.id + ".jpg");
}

if(bitmap != null)
if (bitmap != null)
{
synchronized (lock)
try
{
synchronized (lock)
{
getImageCache(param.context).put(param.id, bitmap);
}
}
catch(Exception e)
{
getImageCache(param.context).put(param.id, bitmap);
e.printStackTrace();
}
}
}

if(bitmap != null)
if (bitmap != null)
{
synchronized (getCache())
{
if(getCache().get(param.id) == null)
if (getCache().get(param.id) == null)
{
getCache().put(param.id, bitmap);
}
Expand All @@ -119,14 +134,14 @@ protected Void doInBackground(Object... objects)
protected void onPostExecute(Void aVoid)
{
ImageView iv = param.imageView.get();
if(iv == null)
if (iv == null)
{
return;
}
if(bitmap != null)
if (bitmap != null)
{
iv.setImageBitmap(bitmap);
if(param.callback != null)
if (param.callback != null)
{
param.callback.onBitmap(bitmap);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cz.kofron.foodinventory.client.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceView;

/**
* Created by kofee on 25.4.14.
*/
public class SuspendableSurfaceView extends SurfaceView
{
public SuspendableSurfaceView(Context context)
{
super(context);
}

public SuspendableSurfaceView(Context context, AttributeSet attrs)
{
super(context, attrs);
}

public SuspendableSurfaceView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 30 additions & 3 deletions src/Client/client/src/main/res/layout/scan_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
android:weightSum="1"
android:id="@+id/container">

<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/content">

<RelativeLayout
android:layout_width="fill_parent"
Expand All @@ -19,18 +24,40 @@
android:layout_alignParentStart="true"
android:layout_alignParentRight="true">

<include layout="@layout/scan_bottom" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include layout="@layout/scan_bottom" />

</LinearLayout>

</RelativeLayout>

<SurfaceView
<cz.kofron.foodinventory.client.view.SuspendableSurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/relative_layout"
android:layout_gravity="center_horizontal"/>


</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/camera_swap_button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="@drawable/device_access_switch_camera"/>
</RelativeLayout>

</RelativeLayout>

</LinearLayout>

0 comments on commit 5f07abf

Please sign in to comment.