Skip to content

Commit

Permalink
获取流转化为图片
Browse files Browse the repository at this point in the history
  • Loading branch information
DuskSunShine committed Nov 28, 2018
1 parent 469cf0a commit e79c42f
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 15 deletions.
29 changes: 29 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 26 additions & 5 deletions app/src/main/java/com/scy/cameralib/CameraActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
Expand All @@ -32,7 +33,7 @@ public class CameraActivity extends Activity implements SurfaceHolder.Callback
private Camera camera;
ImageView image;
ImageView image2;
ViewfinderView maskView;
RectViewfinderView maskView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -71,9 +72,13 @@ protected void onResume() {
cameraManager.setOnPreviewFrameListener(new CameraManager.OnPreviewFrameListener() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {

PlanarYUVLuminanceSource source = cameraManager
.buildLuminanceSource(data, cameraManager.getCameraResolution().x, cameraManager.getCameraResolution().y);
bundleThumbnail(source);
//这里byte数据即是实时获取的帧数据 只要相机正在预览就会一直回调此方法
//需要注意的是 这里的byte数据不能够直接使用 需要转换下格式
Bitmap bmp = null;
/*Bitmap bmp = null;
try {
YuvImage image = new YuvImage(data, ImageFormat.NV21, 480, 640, null);
if (image != null) {
Expand All @@ -85,7 +90,7 @@ public void onPreviewFrame(byte[] data, Camera camera) {
}
} catch (Exception ex) {
Log.e("Sys", "Error:" + ex.getMessage());
}
}*/
}
});
image2.setOnClickListener(new View.OnClickListener() {
Expand All @@ -99,11 +104,27 @@ public void onClick(View v) {
@Override
public void onTakePhoto(String path) {
Log.i(TAG,path);
Bitmap bitmap = BitmapFactory.decodeFile(path);
image2.setImageBitmap(bitmap);
}
});
}

private void bundleThumbnail(PlanarYUVLuminanceSource source) {
int[] pixels = source.renderThumbnail();
int width = source.getThumbnailWidth();
int height = source.getThumbnailHeight();
Bitmap bitmap = Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
Matrix matrix = new Matrix();
matrix.preRotate(90);
bitmap=Bitmap.createBitmap(bitmap,0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);


// Mutable copy:
//barcode = barcode.copy(Bitmap.Config.ARGB_8888, true);
CameraActivity.this.image.setImageBitmap(bitmap);
}
@Override
protected void onPause() {
cameraManager.stopPreview();
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/scy/cameralib/CameraManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public void setOpenAutoFocus(boolean openAutoFocus) {
this.openAutoFocus = openAutoFocus;
}

public Point getCameraResolution() {
return cameraResolution;
}

private CameraManager() {
}

Expand Down Expand Up @@ -275,6 +279,9 @@ public synchronized boolean isOpen() {
return mCamera != null;
}

public Point getPreviewSizeOnScreen() {
return previewSizeOnScreen;
}

/**
* 设置期望的相机参数
Expand Down Expand Up @@ -431,12 +438,15 @@ public synchronized Rect getFramingRect() {
return null;
}

int width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
/* int width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
int height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
int leftOffset = (screenResolution.x - width) / 2;
int topOffset = (screenResolution.y - height) / 2;
framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);*/
int leftOffset = (screenResolution.x ) / 2;
int topOffset = (screenResolution.y ) / 2;
framingRect = new Rect(leftOffset-200, topOffset-200, leftOffset + 200, topOffset + 200);
Log.d(TAG, "Calculated framing rect: " + framingRect);
}
return framingRect;
Expand Down
31 changes: 26 additions & 5 deletions app/src/main/java/com/scy/cameralib/CameraSurfaceView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
import java.io.BufferedOutputStream;
import java.io.File;
Expand All @@ -24,6 +27,7 @@ public class CameraSurfaceView extends SurfaceView {
private String fileDir="CameraLib";//dir name
private OnTakePhotoListener onTakePhotoListener;
private CameraManager cameraManager;
private Rect framingRect;


public String getFileDir() {
Expand Down Expand Up @@ -105,6 +109,7 @@ public void keepTakePicture() {
*/
public void takePicture() {
if (cameraManager!=null) {
framingRect = cameraManager.getFramingRect();
if (cameraManager.getCamera()!=null) {
cameraManager.getCamera().takePicture(null, null, pictureCallback);
}
Expand All @@ -124,16 +129,32 @@ public void onPictureTaken(final byte[] data, Camera Camera) {
public void run() {
BufferedOutputStream bos = null;
Bitmap bm = null;
Bitmap rectBitmap=null;
try {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
bm = BitmapFactory.decodeByteArray(data, 0, data.length, newOpts);
Matrix matrix = new Matrix();
matrix.preRotate(90);
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, false);

Log.i("拍照原始尺寸",bm.getWidth()+","+bm.getHeight());
//旋转后rotaBitmap是960×1280.预览surfaview的大小是540×800
//将960×1280缩放到540×800
int width=framingRect.right-framingRect.left;
int height=framingRect.bottom-framingRect.top;
Point bestPreviewSize = cameraManager.getPreviewSizeOnScreen();
Bitmap sizeBitmap = Bitmap.createScaledBitmap(bm, bestPreviewSize.x, bestPreviewSize.y, true);
Log.i("拍照缩放到surface大小",sizeBitmap.getWidth()+","+sizeBitmap.getHeight());
Log.i("矩形取景框区域",framingRect.toString());
if (framingRect.left+width>sizeBitmap.getWidth()){
width=sizeBitmap.getWidth();
}
if (framingRect.top+height>sizeBitmap.getHeight()){
height=sizeBitmap.getHeight();
}
rectBitmap = Bitmap.createBitmap(sizeBitmap, framingRect.left, framingRect.top, width, height);//截取
bos = new BufferedOutputStream(new FileOutputStream(picFile));
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
rectBitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
} catch (Exception e) {
e.printStackTrace();
} finally {
Expand All @@ -142,9 +163,9 @@ public void run() {
bos.flush();
bos.close();
}
if (bm != null) {
bm.recycle();
}
if (rectBitmap!=null){
rectBitmap.recycle();
}
new MediaScanner(getContext()).scanFile(RootFilePath(), "image/*");
} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public final class ViewfinderView extends View {
public final class RectViewfinderView extends View {

private static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
private static final int CURRENT_POINT_OPACITY = 0xA0;
Expand All @@ -30,7 +30,7 @@ public final class ViewfinderView extends View {
private int scannerAlpha;

// This constructor is used when the class is built from an XML resource.
public ViewfinderView(Context context, AttributeSet attrs) {
public RectViewfinderView(Context context, AttributeSet attrs) {
super(context, attrs);

// Initialize these once for performance rather than calling them every time in onDraw().
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/scy/cameralib/camera/CameraConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.scy.cameralib.camera;

/**
* Created by SCY on 2018/11/28 at 17:32.
*/
public interface CameraConfig {


}
8 changes: 8 additions & 0 deletions app/src/main/java/com/scy/cameralib/camera/CameraManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.scy.cameralib.camera;

/**
* Created by SCY on 2018/11/28 at 17:33.
*/
public class CameraManager {

}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.scy.cameralib.ViewfinderView
<com.scy.cameralib.RectViewfinderView
android:id="@+id/maskView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Expand Down

0 comments on commit e79c42f

Please sign in to comment.