2013-05-16 21 views
16

アプリのメインビューが画面に表示されている間、AndroidアプリでQRコードをスキャンする必要があります。メインビューには、カメラプレビュー付きのウィンドウが含まれている必要がありますが、フルスクリーンカメラプレビューはありません。フルスクリーンカメラを使用しないQRコード

使用例:スキャンされたQRコードとカメラプレビューのリストを含むメインビュー。新しいQRコードがスキャンされると、それがリストに追加されます。

可能ですか?

+0

を開くことなく、バックグラウンドから画像をキャプチャすることができますが、ZBar Androidの例を見てくださいhttps://github.com/ZBar/ZBar/ツリー/マスター/アンドロイド – Marware

答えて

3

私は完全に動作する例はありませんが、カメラのプレビューをフルスクリーンよりも小さいビューで表示するプロジェクトのスニペットを提供できます。私はそのアイディアを伝えたいだけです。あなたが必要なもの

は、今、私たちはまた、

import java.io.IOException; 

import android.content.Context; 
import android.hardware.Camera; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

/** A basic Camera preview class */ 
public class CameraPreviewListener extends SurfaceView implements SurfaceHolder.Callback { 
    private SurfaceHolder mHolder; 
    private Camera mCamera; 

    public CameraPreviewListener(Context context, Camera camera) { 
     super(context); 
     mCamera = camera; 

     // Install a SurfaceHolder.Callback so we get notified when the 
     // underlying surface is created and destroyed. 
     mHolder = getHolder(); 
     mHolder.addCallback(this); 

     // deprecated setting, but required on Android versions prior to 3.0 
     mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     // The Surface has been created, now tell the camera where to draw the preview. 
     mCamera.setPreviewDisplay(holder); 
     mCamera.startPreview(); 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     // Take care of releasing the Camera preview in your activity. 
     Log.d("camera", "surfaceDestroyed"); 
     if(holder.equals(mHolder)){ 
      holder.removeCallback(this);    
     }else{ 
      holder.removeCallback(this); 
      mHolder.removeCallback(this); 
     } 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
     // If your preview can change or rotate, take care of those events here. 
     // Make sure to stop the preview before resizing or reformatting it. 

     if (mHolder.getSurface() == null){ 
      // preview surface does not exist 
      return; 
     } 

     // stop preview before making changes 
     try { 
      mCamera.stopPreview(); 

     } catch (Exception e){ 
      e.printStackTrace(); 
      // ignore: tried to stop a non-existent preview 
     } 

     // set preview size and make any resize, rotate or 
     // reformatting changes here 

     // start preview with new settings 
     try { 
      mCamera.setPreviewDisplay(mHolder); 
      mCamera.startPreview(); 

     } catch (Exception e){ 
      Log.d("camera", "Error starting camera preview: " + e.getMessage()); 
     } 
    } 

    public void removeCallback(){ 
     mHolder = getHolder(); 
     mHolder.removeCallback(this); 
    } 
} 

が最後にあなたがあなたの活動

のすべてを組み立てるために必要なビューであるPreviewListenerを必要とするカメラプレビュー

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/absoluteLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/transparent" 
    android:orientation="vertical" > 

    <FrameLayout 
     android:id="@+id/camera_preview" 
     android:layout_width="200dp" 
     android:layout_height="200dip" > 
    </FrameLayout> 

</RelativeLayout> 

を保持するでframeLayoutです

import android.hardware.Camera; 

Camera mCamera = = getCameraInstance(); 
CameraPreviewListener cpl = new CameraPreviewListener(this, mCamera); 
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
preview.addView(cpl); 

カメラを入手するには、次の方法を使用します

/** A safe way to get an instance of the Camera object. */ 
public Camera getCameraInstance() { 
    Camera c = null; 
    try { 
     c = Camera.open(); // attempt to get a Camera instance 
     Parameters p = c.getParameters(); 
     List<Size> sizes = p.getSupportedPictureSizes(); 

     Size x = null; 

     if (sizes.size() < 1) { 
      throw new Exception("there are not supported picture sizes at all !!!"); 
     } 

     for (Size s : sizes) { 
      if (s.width == 640 && s.height == 480) { 
       x = s; 
      } 
     } 

     if (x == null) { 
      x = sizes.get(0); 
      p.setPictureSize(x.width, x.height); 
     } else { 
      p.setPictureSize(640, 480); 
     } 
     p.setJpegQuality(20); 
     p.setGpsLatitude(MapViewer.latitude); 
     p.setGpsLongitude(MapViewer.longitude); 
     c.setParameters(p); 
    } catch (Exception e) { 
     // Camera is not available (in use or does not exist) 
     Log.d(TAG + "(getCameraInstance)", e.getMessage()); 
    } 
    return c; // returns null if camera is unavailable 
} 
関連する問題