gpt4 book ai didi

android - Android 19-22 的 ImageWriter 替代品?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:34 25 4
gpt4 key购买 nike

在第 23 版的 android API 中引入了类 ImageWriter .我需要在应在 api 19 上运行的应用程序中使用此类。我怎样才能重新实现类(class)?是否有一些等效的代码(我有一个 Image 实例需要绘制到表面上)?

最佳答案

这里有一段代码,可以让您拍照并在 Surface 上预览。我相信您可以根据您的目的调整代码。

这是 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dip"
android:layout_alignParentBottom="true"
android:gravity="center_vertical"
android:background="#A000">
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="Cancel"
android:onClick="onCancelClick"
/>
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Snap Photo"
android:onClick="onSnapClick"
/>
</RelativeLayout>
</RelativeLayout>

这里是 Java 代码:

package app.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;


public class PreviewActivity extends Activity implements SurfaceHolder.Callback, Camera.ShutterCallback, Camera.PictureCallback {

Camera mCamera;
SurfaceView mPreview;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mPreview = (SurfaceView)findViewById(R.id.preview);
mPreview.getHolder().addCallback(this);
mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

mCamera = Camera.open();
}

@Override
public void onPause() {
super.onPause();
mCamera.stopPreview();
}

@Override
public void onDestroy() {
super.onDestroy();
mCamera.release();
Log.d("CAMERA","Destroy");
}

public void onCancelClick(View v) {
finish();
}

public void onSnapClick(View v) {
mCamera.takePicture(this, null, null, this);
}

@Override
public void onShutter() {
Toast.makeText(this, "Click!", Toast.LENGTH_SHORT).show();
}

@Override
public void onPictureTaken(byte[] data, Camera camera) {
//Here, we chose internal storage
try {
FileOutputStream out = openFileOutput("picture.jpg", Activity.MODE_PRIVATE);
out.write(data);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
Camera.Size selected = sizes.get(0);
params.setPreviewSize(selected.width,selected.height);
mCamera.setParameters(params);

mCamera.setDisplayOrientation(90);
mCamera.startPreview();
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(mPreview.getHolder());
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("PREVIEW","surfaceDestroyed");
}
}

关于android - Android 19-22 的 ImageWriter 替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32251722/

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