更新了更多代码
我正在尝试抓取刚拍摄的照片并将其设置为ImageView
编程.
按下图片按钮,
picture_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
takePhoto(mTextureView);
}
});
Run Code Online (Sandbox Code Playgroud)
它运行takePhoto方法:
public void takePhoto(View view) {
try {
mImageFile = createImageFile();
final ImageView latest_picture = (ImageView) findViewById(R.id.latest_picture);
final RelativeLayout latest_picture_container = (RelativeLayout) findViewById(R.id.latest_picture_container);
final String mImageFileLocationNew = mImageFileLocation.replaceFirst("^/", "");
Toast.makeText(getApplicationContext(), "" + mImageFile, Toast.LENGTH_SHORT).show();
Uri uri = Uri.fromFile(mImageFile);
Toast.makeText(getApplicationContext(), ""+uri, Toast.LENGTH_LONG).show();
latest_picture.setImageURI(uri);
latest_picture_container.setVisibility(View.VISIBLE);
} catch (IOException e){
e.printStackTrace();
}
lockFocus();
captureStillImage();
}
Run Code Online (Sandbox Code Playgroud)
哪个运行createImageFile()
和captureStillImage()
private void captureStillImage() {
try {
CaptureRequest.Builder captureStillBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureStillBuilder.addTarget(mImageReader.getSurface());
int rotation = getWindowManager().getDefaultDisplay().getRotation();
captureStillBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
//Toast.makeText(getApplicationContext(), "Image Taken", Toast.LENGTH_SHORT).show();
unLockFocus();
}
};
mCameraCaptureSession.capture(captureStillBuilder.build(), captureCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
File createImageFile() throws IOException {
String timestamp = new SimpleDateFormat("yyyyMMdd").format(new Date());
String subFolder = "";
if(pref_session_unique_gallery.equals("yes")){
if(event_name != null){
subFolder = event_name;
} else {
subFolder = timestamp;
}
} else {
subFolder = "_GEN";
}
if(event_name == null){
event_name = "";
} else {
event_name = event_name + "_";
}
String imageFileName = "CPB_"+event_name+timestamp+"_";
File storageDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "CPB" + File.separator + subFolder);
storageDirectory.mkdir();
File image = File.createTempFile(imageFileName, ".jpg", storageDirectory);
mImageFileLocation = image.getAbsolutePath();
return image;
}
Run Code Online (Sandbox Code Playgroud)
和图像保存在这里:
private static class ImageSaver implements Runnable {
private final Image mImage;
private ImageSaver(Image image) {
mImage = image;
}
@Override
public void run() {
ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(mImageFile);
fileOutputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
if(fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在获取图像的正确路径createImageFile()
并烘烤它以显示它每次都是什么.但是,即使不setVisibility
上latest_picture_container
工作...如果我注释掉InputStream
,Bitmap
并且setImageBitmap
,然后latest_picture_container
显示为正常.不知道为什么这是错的.
出于某种原因,URI在文件后面会有三个斜杠,
file:///storage/0/...
Run Code Online (Sandbox Code Playgroud)
该AssetManager#open()
方法仅适用于您应用的资产;即,项目/assets
文件夹中的文件。由于您尝试打开外部文件,因此您应该在从路径创建FileInputStream
的File
对象上使用 a 。此外,该takePhoto()
方法在将ImageView
任何内容写入文件之前设置图像,这将导致空的ImageView
.
由于您的应用程序直接使用相机,我们可以从写入图像文件的相同字节数组中解码图像,并为自己节省不必要的存储读取。此外,在您的代码示例中,文件写入发生在单独的线程上,因此我们不妨在执行图像解码时利用这一点,因为它将最大限度地减少对 UI 线程的影响。
首先,我们将创建一个接口,通过该接口ImageSaver
可以将图像传回Activity
以显示在ImageView
.
public interface OnImageDecodedListener {
public void onImageDecoded(Bitmap b);
}
Run Code Online (Sandbox Code Playgroud)
然后我们需要ImageSaver
稍微改变类以Activity
在构造函数中接受一个参数。我还添加了一个File
参数,因此Activity
的相应字段不必是static
。
private static class ImageSaver implements Runnable {
private final Activity mActivity;
private final Image mImage;
private final File mImageFile;
public ImageSaver(Activity activity, Image image, File imageFile) {
mActivity = activity;
mImage = image;
mImageFile = imageFile;
}
@Override
public void run() {
ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
final Bitmap b = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
((OnImageDecodedListener) mActivity).onImageDecoded(b);
}
}
);
FileOutputStream fileOutputStream = null;
...
}
}
Run Code Online (Sandbox Code Playgroud)
一旦我们获得字节数组中的图像数据,我们就对其进行解码并将其传递回Activity
,因此它不必等待文件写入,可以在后台安静地进行。我们需要在 UI 线程上调用 interface 方法,因为我们正在“触摸”View
那里。
在Activity
将需要实现的接口,我们可以移动的View
,从产权相关的东西takePhoto()
的onImageDecoded()
方法。
public class MainActivity extends Activity
implements ImageSaver.OnImageDecodedListener {
...
@Override
public void onImageDecoded(Bitmap b) {
final ImageView latest_picture =
(ImageView) findViewById(R.id.latest_picture);
final RelativeLayout latest_picture_container =
(RelativeLayout) findViewById(R.id.latest_picture_container);
latest_picture.setImageBitmap(b);
latest_picture_container.setVisibility(View.VISIBLE);
}
public void takePhoto(View view) {
try {
mImageFile = createImageFile();
captureStillImage();
lockFocus();
}
catch (IOException e) {
e.printStackTrace();
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
最后,我们需要实际执行ImageSaver
inImageReader
的onImageAvailable()
方法。再次按照这个例子,它会是这样的。
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener =
new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
mBackgroundHandler.post(new ImageSaver(MainActivity.this,
reader.acquireNextImage(),
mImageFile)
);
}
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10606 次 |
最近记录: |