我正在开发应用程序,从我的自定义相机类拍摄照片,然后采取路径并将其放入活动中的imageView,就像预览图像一样,现在我创建了一个处理相机功能的类并向活动发送路径预览结果。但我的结果并不是我拍摄的正确照片。例如:我第一次拍照我的" currentPicpath"是空的,但在第二次我拍照时,它给了我之前拍摄的第一张图片。
所以,在类2中,我创建了一个获取Current路径的方法但钢铁不给出null除非拍摄新照片。
还有一个问题。为什么在保存图像之后它们却相反? 我的课程:
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_additem);
d_image_pre1 = (ImageView) findViewById(R.id.d_image1);
d_BTakePicture = (Button) findViewById(R.id.d_bTakePicture);
bOpenCamera = (Button) findViewById(R.id.bOpenCamera);
d_BTakePicture.setOnClickListener(this);
bOpenCamera.setOnClickListener(this);
take = new TakeApicture(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bOpenCamera:
take.openCam();
break;
case R.id.d_bTakePicture:
take.makeFolder("myTest");
take.captureImage();
String path = take.getCurrentPicPath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(path, options);
d_image_pre1.setImageBitmap(bm);
break;
default:
break;
}
}
第2课:
public class TakeApicture implements SurfaceHolder.Callback{
Activity context;
Camera camera;
SurfaceView surface;
SurfaceHolder holder;
PictureCallback jpegCallback;
File myGeneralFolder;
FileOutputStream outStream = null;
private String fullPathFolder;
String currentPicPath = "No image path";
@SuppressWarnings("deprecation")
public TakeApicture(Activity context) {
super();
this.context = context;
surface = (SurfaceView)context.findViewById(R.id.surfaceView);
holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
jpegCallBack();
}
public void captureImage() {
camera.takePicture(null, null, jpegCallback);
}
public void makeFolder(String itemFolderName) {
fullPathFolder = Environment.getExternalStorageDirectory()+File.separator+"mySalesImages"+File.separator+itemFolderName;
myGeneralFolder = new File(fullPathFolder);
myGeneralFolder.mkdirs();
}
public void jpegCallBack(){
jpegCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
getPicPath(data);
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
public void getPicPath(byte[] data) throws IOException{
currentPicPath = String.format(myGeneralFolder+"/%d.jpg",(System.currentTimeMillis()));
outStream = new FileOutputStream(currentPicPath);
outStream.write(data);
outStream.close();
}
public String getCurrentPicPath() {
return currentPicPath;
}
@SuppressWarnings("deprecation")
public void openCam(){
try {
camera = Camera.open();
Camera.Parameters param;
param = camera.getParameters();
//modify parameter
camera.setDisplayOrientation(90);
param.setPreviewFrameRate(20);
param.setPreviewSize(176, 144);
camera.setParameters(param);
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (Exception e) {
// TODO: handle exception
}
}
public void closeCam(){
camera.stopPreview();
camera.release();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
closeCam();
}
}
这是正确的解决方案???
take.captureImage();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
String path = take.getCurrentPicPath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(path, options);
d_image_pre1.setImageBitmap(bm);
}
}, 1000);
答案 0 :(得分:0)
行take.captureImage();
启动捕获照片的异步过程。一段时间后,Android系统将调用您的onPictureTaken()
回调,您将计算新的图像路径(并相应地写入照片)。但行
String path = take.getCurrentPicPath();
已经执行过了。
您可以同步计算路径,但即使这样,您的Activity也必须等待将实际图像写入磁盘。因此,你别无选择,只能提取作品
String path = take.getCurrentPicPath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(path, options);
d_image_pre1.setImageBitmap(bm);
进入单独的方法。您可以直接从onPictureTaken()
调用此新方法,也可以使用post()
(不需要来自postDelayed()
的{{1}})来异步执行。
因此,快速而肮脏的修复(为简洁而删除了异常处理)如下:
MainActivity.java :中的
onPictureTaken()
TakeApicture.java :
public void onClick(View v) {
switch (v.getId()) {
case R.id.bOpenCamera:
take.openCam();
break;
case R.id.d_bTakePicture:
take.makeFolder("myTest");
take.captureImage();
break;
}
}
public void setImage(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(path, options);
d_image_pre1.setImageBitmap(bm);
}