预览的时候 没事拍完了就变的 瘪了求助。为什么呢???
类camera1
package com.camera1;import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.location.Location;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.OrientationEventListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class camera1 extends Activity implements SurfaceHolder.Callback {
private Camera mCamera;
private boolean mPreviewRunning=true;
private long mFocusStartTime;
private long mFocusCallbackTime;
private long mCaptureStartTime;
private long mShutterCallbackTime;
private long mRawPictureCallbackTime;
private int mPicturesRemaining;
private boolean mRecordLocation;
private boolean mKeepAndRestartPreview;
private boolean mIsImageCaptureIntent;
private boolean save=true;
public static final int ITEM0 = Menu.FIRST;
public static final int ITEM1 = Menu.FIRST + 1;
public static final int ITEM2 = Menu.FIRST + 2;
byte[] data;
private int mLastOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.com_camera1_main);
SurfaceView mSurfaceView;
ImageButton shutter;
ImageButton finish; mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
shutter = (ImageButton) findViewById(R.id.shutter);
finish = (ImageButton) findViewById(R.id.finish);
shutter.setOnClickListener(shutter1);
finish.setOnClickListener(finish1);
// buildDialog("camera");
SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//mParameters.set("rotation",latchedOrientation);
//Camera.Parameters ss = mCamera.getParameters();
// ss.set("rotation",mLastOrientation + 90);
//mCamera.setParameters(ss);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0,ITEM0,0,"摄像");
menu.add(0,ITEM1,1,"设置");
menu.add(0,ITEM2,2,"退出");
menu.findItem(ITEM1);
return true;
}
private OnClickListener shutter1 = new OnClickListener() { @Override
public void onClick(View ic_camera_indicator_photo) {
// TODO Auto-generated method stub
mCamera.takePicture( shutter2, raw, jpegCallback );
}
};
private OnClickListener finish1 = new OnClickListener() { @Override
public void onClick(View shezhi) {
// TODO Auto-generated method stub
camera1.super.openOptionsMenu();
}
};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
// TODO Auto-generated method stub
if (mPreviewRunning)
{ mCamera.stopPreview(); } Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(w, h);
p.set("rotation",90);
mCamera.setParameters(p); try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } mCamera.startPreview(); mPreviewRunning = true;
} @Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera = Camera.open();
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera.stopPreview(); mPreviewRunning = false; mCamera.release();
}
Camera.PictureCallback raw = new Camera.PictureCallback() { public void onPictureTaken(byte[] imageData, Camera c) { } };
private PictureCallback jpegCallback = new PictureCallback() { public void onPictureTaken(byte[] _data, Camera _camera) { // TODO Handle JPEG image data /* onPictureTaken传入的第一个参数即为相片的byte */
data= _data;
showDialog(1);
// mCamera.startPreview(); }
};
/* Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] imageData, Camera c) { Bitmap bitmap=BitmapFactory.decodeByteArray(imageData, 0, imageData.length); if(bitmap!=null){
try {
String nativefile;
nativefile="szl";
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("/sdcard/images/"+ nativefile));
bitmap.compress(CompressFormat.JPEG, 75, bos);
bos.flush();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
}
}
}
};*/
Camera.ShutterCallback shutter2=new Camera.ShutterCallback() {
public void onShutter() {
}
};
private Dialog buildDialog(Context context){
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("是否储存照片");
builder.setPositiveButton("存储",new DialogInterface.OnClickListener(
){
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
savepicture(data);
}
});
builder.setNegativeButton("取消",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mCamera.startPreview();
}
});
return builder.create();
}; public Dialog onCreateDialog(int i)
{
switch(i){
case 1:
return buildDialog(camera1.this);
}
return null;
};
public void savepicture(byte[] _data)
{
Bitmap bm = BitmapFactory.decodeByteArray(_data, 0, _data.length); /* 创建新文件 */ File myCaptureFile = new File("/sdcard/" + System.currentTimeMillis() + ".jpg" ); try { BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream(myCaptureFile)); bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); /* 采用压缩转档方法 */ bos.flush(); bos.close(); Thread.sleep(3000); mCamera.startPreview(); } catch (Exception e) { Log.e("camera", e.getMessage()); }
Toast.makeText(camera1.this, "照片存储路径:"+"/sdcard/" + System.currentTimeMillis() + ".jpg",
Toast.LENGTH_LONG).show();
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case ITEM0:
break;
case ITEM1:
break;
case ITEM2:
break;
}
return super.onOptionsItemSelected(item);
}
}
类camera1
package com.camera1;import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.location.Location;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.OrientationEventListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class camera1 extends Activity implements SurfaceHolder.Callback {
private Camera mCamera;
private boolean mPreviewRunning=true;
private long mFocusStartTime;
private long mFocusCallbackTime;
private long mCaptureStartTime;
private long mShutterCallbackTime;
private long mRawPictureCallbackTime;
private int mPicturesRemaining;
private boolean mRecordLocation;
private boolean mKeepAndRestartPreview;
private boolean mIsImageCaptureIntent;
private boolean save=true;
public static final int ITEM0 = Menu.FIRST;
public static final int ITEM1 = Menu.FIRST + 1;
public static final int ITEM2 = Menu.FIRST + 2;
byte[] data;
private int mLastOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.com_camera1_main);
SurfaceView mSurfaceView;
ImageButton shutter;
ImageButton finish; mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
shutter = (ImageButton) findViewById(R.id.shutter);
finish = (ImageButton) findViewById(R.id.finish);
shutter.setOnClickListener(shutter1);
finish.setOnClickListener(finish1);
// buildDialog("camera");
SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//mParameters.set("rotation",latchedOrientation);
//Camera.Parameters ss = mCamera.getParameters();
// ss.set("rotation",mLastOrientation + 90);
//mCamera.setParameters(ss);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0,ITEM0,0,"摄像");
menu.add(0,ITEM1,1,"设置");
menu.add(0,ITEM2,2,"退出");
menu.findItem(ITEM1);
return true;
}
private OnClickListener shutter1 = new OnClickListener() { @Override
public void onClick(View ic_camera_indicator_photo) {
// TODO Auto-generated method stub
mCamera.takePicture( shutter2, raw, jpegCallback );
}
};
private OnClickListener finish1 = new OnClickListener() { @Override
public void onClick(View shezhi) {
// TODO Auto-generated method stub
camera1.super.openOptionsMenu();
}
};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
// TODO Auto-generated method stub
if (mPreviewRunning)
{ mCamera.stopPreview(); } Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(w, h);
p.set("rotation",90);
mCamera.setParameters(p); try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } mCamera.startPreview(); mPreviewRunning = true;
} @Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera = Camera.open();
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera.stopPreview(); mPreviewRunning = false; mCamera.release();
}
Camera.PictureCallback raw = new Camera.PictureCallback() { public void onPictureTaken(byte[] imageData, Camera c) { } };
private PictureCallback jpegCallback = new PictureCallback() { public void onPictureTaken(byte[] _data, Camera _camera) { // TODO Handle JPEG image data /* onPictureTaken传入的第一个参数即为相片的byte */
data= _data;
showDialog(1);
// mCamera.startPreview(); }
};
/* Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] imageData, Camera c) { Bitmap bitmap=BitmapFactory.decodeByteArray(imageData, 0, imageData.length); if(bitmap!=null){
try {
String nativefile;
nativefile="szl";
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("/sdcard/images/"+ nativefile));
bitmap.compress(CompressFormat.JPEG, 75, bos);
bos.flush();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
}
}
}
};*/
Camera.ShutterCallback shutter2=new Camera.ShutterCallback() {
public void onShutter() {
}
};
private Dialog buildDialog(Context context){
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("是否储存照片");
builder.setPositiveButton("存储",new DialogInterface.OnClickListener(
){
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
savepicture(data);
}
});
builder.setNegativeButton("取消",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mCamera.startPreview();
}
});
return builder.create();
}; public Dialog onCreateDialog(int i)
{
switch(i){
case 1:
return buildDialog(camera1.this);
}
return null;
};
public void savepicture(byte[] _data)
{
Bitmap bm = BitmapFactory.decodeByteArray(_data, 0, _data.length); /* 创建新文件 */ File myCaptureFile = new File("/sdcard/" + System.currentTimeMillis() + ".jpg" ); try { BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream(myCaptureFile)); bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); /* 采用压缩转档方法 */ bos.flush(); bos.close(); Thread.sleep(3000); mCamera.startPreview(); } catch (Exception e) { Log.e("camera", e.getMessage()); }
Toast.makeText(camera1.this, "照片存储路径:"+"/sdcard/" + System.currentTimeMillis() + ".jpg",
Toast.LENGTH_LONG).show();
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case ITEM0:
break;
case ITEM1:
break;
case ITEM2:
break;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/camera"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<SurfaceView android:id="@+id/surface_camera" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal"
android:screenOrientation="landscape" android:layout_weight="1"> </SurfaceView>
<ImageButton android:id="@+id/shutter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_camera_indicator_photo"
android:clickable="true"
android:focusable="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
>
</ImageButton>
<ImageButton android:id="@+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shezhi"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="5dip"
android:layout_marginBottom="5dip"
android:scaleType="center"
>
</ImageButton>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.camera1"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".camera1"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"
android:clearTaskOnLaunch="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-sdk android:minSdkVersion="8" />
</manifest>
拍完了就变的 “瘪了”是什么意思?
p.setPreviewSize(w, h);把这个值打印出来,或者,你把这个值固定,比如w=600,h=480看看什么效果
p.setPreviewSize(w, h)改成p.setPreviewSize(800,480)看看
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;public class Takephotos extends Activity { private CameraView cv;
private String photoPath;
private Camera mCamera = null;
ImageButton take; public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) {
Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// Matrix matrix = new Matrix();
// matrix.postScale(0.05f, 0.05f);
// Bitmap newBitmap=Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
File myCaptureFile = new File(getFilesDir(), photoPath);
try {
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(
myCaptureFile));
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
resetCamera(); Intent intent = new Intent();
intent.putExtra("photopath", myCaptureFile.getPath());
setResult(RESULT_OK, intent);
finish();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (mBitmap != null) {
mBitmap.recycle();
}
}
}
}; //reret Camera
private void resetCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.takephoto);
// cv = new CameraView(this);
// FrameLayout fl = new FrameLayout(this);
// fl.addView(cv);
// setContentView(fl);
LinearLayout ll=(LinearLayout) findViewById(R.id.cameralayout);
cv = new CameraView(this);
ll.addView(cv);
Bundle extras=getIntent().getExtras();
if(extras!=null){
if(extras.getString("driver")!=null){
photoPath=extras.getString("driver");
}
else if(extras.getString("truck")!=null){
photoPath=extras.getString("truck");
}
}
//
take=(ImageButton) findViewById(R.id.take);
take.setClickable(true);
take.getBackground().setAlpha(100);
take.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mCamera != null) {
mCamera.takePicture(null, null, pictureCallback);
take.setClickable(false);
}
} });
} public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
if (mCamera != null) {
mCamera.takePicture(null, null, pictureCallback);
}
} if(keyCode==KeyEvent.KEYCODE_BACK){
finish();
}
return cv.onKeyDown(keyCode, event);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
super.onPause();
resetCamera();
} // set the Camera
class CameraView extends SurfaceView { private SurfaceHolder holder = null; public CameraView(Context context) {
super(context);
holder = this.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
} @Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
mCamera.release();
mCamera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
// Set images format
parameters.setPictureFormat(PixelFormat.JPEG);
// set Preview size
// WindowManager manager=(WindowManager) getSystemService(Context.WIFI_SERVICE);
// Display display=manager.getDefaultDisplay();
// parameters.setPreviewSize(320, 480);
// parameters.setPreviewSize(display.getWidth(), display.getHeight());
//set automatic focus
// parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
// Setting the picture the resolution of the preservation size
//parameters.setPictureSize(320, 240);
// set the data of Camera
mCamera.setParameters(parameters);
// start preview
mCamera.startPreview();
}
});
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} }}
[email protected]
谢谢了
预览正常,拍摄后出问题。
我以前调试spc110平台,android2.2\2.3系统的注意看两个地方:
1,你摄像头初始化的分辨率,如下:(不同的摄像头寄存器写法不一样)
{0x0F12,0x0640}, //REG_0TC_PCFG_usWidth
{0x0F12,0x04b0}, //REG_0TC_PCFG_usHeight
2,libcamera这个文件夹中SecCamera.h中对应硬件接口层参数的设定,这个与与上层关系不大。
如果你是三星平台的话,android2.2的版本会出现这种问题。后面打一个path就好了,要改动的比较多。
android2.3里面就没这个问题。
我现在发现我做的camera还没有防抖