gpt4 book ai didi

android - 如何在android中使用surfaceview类捕获后在imageview中显示图像

转载 作者:搜寻专家 更新时间:2023-11-01 08:49:32 24 4
gpt4 key购买 nike

我有使用自定义相机 API 捕获图像的代码,问题是它不会返回预览模式,主要是我想在 imageview 中显示捕获的图像"

这是我的代码:

CameraPreview.java

import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusMoveCallback;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;

// Constructor that obtains context and camera
@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
super(context);
this.mCamera = camera;
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(this);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
mCamera.setDisplayOrientation(90);
mCamera.startPreview();

Camera.Parameters prams = mCamera.getParameters();
prams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

mCamera.setParameters(prams);
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
mCamera.autoFocus(null);

} catch (IOException e) {
// left blank for now
}
}@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
mCamera.stopPreview();
mCamera.release();

}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {





// start preview with new settings
try {
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (Exception e) {
// intentionally left blank for a test
}
}

MainActivity.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class MainActivity extends Activity {

private static final String TAG = MainActivity.class.getName();
private Camera mCamera;
private CameraPreview mCameraPreview;
private Button captureButton;
private ImageView showImageView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCamera = getCameraInstance();
mCameraPreview = new CameraPreview(this, mCamera);

FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);

ImageView showImageView = (ImageView) findViewById(R.id.imageView1);

preview.addView(mCameraPreview);


captureButton = (Button) findViewById(R.id.button_capture);

captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
}
});

}
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();

} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}

PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {

} catch (IOException e) {
}
}
};

private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");

return mediaFile;
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center" >

<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_above="@+id/btns"
android:layout_alignParentTop="true" >

</FrameLayout>

<RelativeLayout
android:id="@+id/btns"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#ccc" >

<Button
android:id="@+id/button_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@drawable/btn_white" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="22dp"
android:src="@drawable/backeff" />

</RelativeLayout>

</RelativeLayout>

最佳答案

您好,请尝试以下代码..

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

public class CameraActivity extends Activity {

private Camera mCamera;
private CameraPreview mCameraPreview;
private Button captureButton;
private ImageView showImageView;
protected String imageFilePath;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_view);
mCamera = getCameraInstance();
mCameraPreview = new CameraPreview(this, mCamera);

FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);

showImageView = (ImageView) findViewById(R.id.imageView1);

preview.addView(mCameraPreview);

captureButton = (Button) findViewById(R.id.button_capture);

captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, jpegCallback);
}
});

}

private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();

} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}

/** The jpeg callback. */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
File miDirs = new File(
Environment.getExternalStorageDirectory() + "/myphotos");
if (!miDirs.exists())
miDirs.mkdirs();

final Calendar c = Calendar.getInstance();
String new_Date = c.get(Calendar.DAY_OF_MONTH) + "-"
+ ((c.get(Calendar.MONTH)) + 1) + "-"
+ c.get(Calendar.YEAR) + " " + c.get(Calendar.HOUR)
+ "-" + c.get(Calendar.MINUTE) + "-"
+ c.get(Calendar.SECOND);

imageFilePath = String.format(
Environment.getExternalStorageDirectory() + "/myphotos"
+ "/%s.jpg", "te1t(" + new_Date + ")");

Uri selectedImage = Uri.parse(imageFilePath);
File file = new File(imageFilePath);
String path = file.getAbsolutePath();
Bitmap bitmap = null;

outStream = new FileOutputStream(file);
outStream.write(data);
outStream.close();

if (path != null) {
if (path.startsWith("content")) {
bitmap = decodeStrem(file, selectedImage,
CameraActivity.this);
} else {
bitmap = decodeFile(file, 10);
}
}
if (bitmap != null) {
showImageView.setImageBitmap(bitmap);
Toast.makeText(CameraActivity.this,
"Picture Captured Successfully:", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(CameraActivity.this,
"Failed to Capture the picture. kindly Try Again:",
Toast.LENGTH_LONG).show();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}

};

/**
* Decode strem.
*
* @param fil
* the fil
* @param selectedImage
* the selected image
* @param mContext
* the m context
* @return the bitmap
*/
public static Bitmap decodeStrem(File fil, Uri selectedImage,
Context mContext) {

Bitmap bitmap = null;
try {

bitmap = BitmapFactory.decodeStream(mContext.getContentResolver()
.openInputStream(selectedImage));

final int THUMBNAIL_SIZE = getThumbSize(bitmap);

bitmap = Bitmap.createScaledBitmap(bitmap, THUMBNAIL_SIZE,
THUMBNAIL_SIZE, false);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(baos
.toByteArray()));

return bitmap = rotateImage(bitmap, fil.getAbsolutePath());

} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}

/**
* Rotate image.
*
* @param bmp
* the bmp
* @param imageUrl
* the image url
* @return the bitmap
*/
public static Bitmap rotateImages(Bitmap bmp, String imageUrl) {
if (bmp != null) {
ExifInterface ei;
int orientation = 0;
try {
ei = new ExifInterface(imageUrl);
orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);

} catch (IOException e) {
e.printStackTrace();
}
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_UNDEFINED:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270);
break;
default:
break;
}
Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
bmpHeight, matrix, true);
return resizedBitmap;
} else {
return bmp;
}
}

/**
* Decode file.
*
* @param f
* the f
* @param sampling
* the sampling
* @param check
* the check
* @return the bitmap
*/
public static Bitmap decodeFile(File f, int sampling) {
try {
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
new FileInputStream(f.getAbsolutePath()), null, o2);

o2.inSampleSize = sampling;
o2.inTempStorage = new byte[48 * 1024];

o2.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(
new FileInputStream(f.getAbsolutePath()), null, o2);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
bitmap = rotateImage(bitmap, f.getAbsolutePath());
return bitmap;

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
return null;
}

/**
* Rotate image.
*
* @param bmp
* the bmp
* @param imageUrl
* the image url
* @return the bitmap
*/
public static Bitmap rotateImage(Bitmap bmp, String imageUrl) {
if (bmp != null) {
ExifInterface ei;
int orientation = 0;
try {
ei = new ExifInterface(imageUrl);
orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);

} catch (IOException e) {
e.printStackTrace();
}
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_UNDEFINED:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270);
break;
default:
break;
}
Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
bmpHeight, matrix, true);
return resizedBitmap;
} else {
return bmp;
}
}


/**
* Gets the thumb size.
*
* @param bitmap
* the bitmap
* @return the thumb size
*/
public static int getThumbSize(Bitmap bitmap) {

int THUMBNAIL_SIZE = 250;
if (bitmap.getWidth() < 300) {
THUMBNAIL_SIZE = 250;
} else if (bitmap.getWidth() < 600) {
THUMBNAIL_SIZE = 500;
} else if (bitmap.getWidth() < 1000) {
THUMBNAIL_SIZE = 750;
} else if (bitmap.getWidth() < 2000) {
THUMBNAIL_SIZE = 1500;
} else if (bitmap.getWidth() < 4000) {
THUMBNAIL_SIZE = 2000;
} else if (bitmap.getWidth() > 4000) {
THUMBNAIL_SIZE = 2000;
}
return THUMBNAIL_SIZE;
}

}

如果您有任何疑问,请告诉我..

关于android - 如何在android中使用surfaceview类捕获后在imageview中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24931881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com