forked from finally-vince/ZXingMini
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YooJia.Chen
committed
Oct 8, 2015
1 parent
cd12555
commit 5e00599
Showing
9 changed files
with
348 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
zxing/src/main/java/com/github/yoojia/zxing/camera/CameraController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.github.yoojia.zxing.camera; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.Matrix; | ||
import android.graphics.Rect; | ||
import android.graphics.YuvImage; | ||
import android.hardware.Camera; | ||
import android.util.Log; | ||
import android.view.SurfaceHolder; | ||
import android.view.SurfaceView; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author 陈小锅 (yoojia.chen@gmail.com) | ||
* @since 1.0 | ||
*/ | ||
public class CameraController { | ||
|
||
private final static String TAG = "CAMERAS"; | ||
|
||
private final CameraManager mCameraManager; | ||
private final SurfaceView mSurfaceView; | ||
|
||
private final CameraSurfaceCallback mCameraSurfaceCallback = new CameraSurfaceCallback() { | ||
@Override | ||
public void surfaceCreated(SurfaceHolder holder) { | ||
initCamera(holder); | ||
} | ||
}; | ||
|
||
private final AutoFocusListener mAutoFocusListener = new AutoFocusListener() { | ||
@Override | ||
public void onFocus(boolean focusSuccess) { | ||
// 对焦成功后,请求触发生成 **一次** 预览图片 | ||
if (focusSuccess) { | ||
mCameraManager.requestPreview(mPreviewCallback); | ||
} | ||
} | ||
}; | ||
|
||
private Camera.PreviewCallback mPreviewCallback; | ||
|
||
public CameraController(SurfaceView surfaceView) { | ||
mCameraManager = new CameraManager(surfaceView.getContext().getApplicationContext()); | ||
mSurfaceView = surfaceView; | ||
} | ||
|
||
/** | ||
* 跟随Activity的生命周期 | ||
*/ | ||
public void resume(){ | ||
final SurfaceHolder holder = mSurfaceView.getHolder(); | ||
holder.addCallback(mCameraSurfaceCallback); | ||
} | ||
|
||
/** | ||
* 跟随Activity的生命周期 | ||
*/ | ||
public void pause(){ | ||
final SurfaceHolder holder = mSurfaceView.getHolder(); | ||
holder.removeCallback(mCameraSurfaceCallback); | ||
mCameraManager.stopPreview(); | ||
mCameraManager.closeDriver(); | ||
} | ||
|
||
public void requestFocus(){ | ||
mCameraManager.getAutoFocusManager().requestAutoFocus(); | ||
} | ||
|
||
public CameraManager getCameraManager() { | ||
return mCameraManager; | ||
} | ||
|
||
public void setPreviewCallback(Camera.PreviewCallback previewCallback) { | ||
mPreviewCallback = previewCallback; | ||
} | ||
|
||
/** | ||
* 生成一张预览图 | ||
*/ | ||
public Bitmap capture(CameraPreview cameraPreview){ | ||
final Camera.Parameters parameters = cameraPreview.camera.getParameters(); | ||
final int width = parameters.getPreviewSize().width; | ||
final int height = parameters.getPreviewSize().height; | ||
final YuvImage yuv = new YuvImage(cameraPreview.data, parameters.getPreviewFormat(), width, height, null); | ||
final ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
yuv.compressToJpeg(new Rect(0, 0, width, height), 100, out);// Best | ||
final byte[] bytes = out.toByteArray(); | ||
final Bitmap src = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); | ||
final Matrix matrix = new Matrix(); | ||
matrix.setRotate(90); | ||
final int originWidth = src.getWidth(); | ||
final int originHeight = src.getHeight(); | ||
final int targetWH = originWidth > originHeight ? originHeight : originWidth; | ||
final int offsetX = originWidth > originHeight ? (originWidth - originHeight): 0; | ||
final int offsetY = originWidth > originHeight ? 0 : (originHeight - originWidth); | ||
return Bitmap.createBitmap(src, offsetX, offsetY, targetWH, targetWH, matrix, true); | ||
} | ||
|
||
private void initCamera(SurfaceHolder holder){ | ||
if (mCameraManager.isOpen()){ | ||
return; | ||
} | ||
try { | ||
mCameraManager.openDriver(holder); | ||
}catch (IOException ioe) { | ||
Log.w(TAG, ioe); | ||
return; | ||
} | ||
mCameraManager.startPreview(mAutoFocusListener); | ||
} | ||
|
||
public static final class CameraPreview{ | ||
|
||
private final byte[] data; | ||
private final Camera camera; | ||
|
||
public CameraPreview(byte[] data, Camera camera) { | ||
this.data = data; | ||
this.camera = camera; | ||
} | ||
} | ||
} |
Oops, something went wrong.